VC#中增么连接数据库,就是把SqlConnection 连接到dbGrid上

CBJ2049011 2003-07-14 07:18:37
VC#中增么连接数据库,就是把SqlConnection 连接到dbGrid上
...全文
22 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪狼1234567 2003-07-14
  • 打赏
  • 举报
回复
简单如下:
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=194910");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from customers",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"customers");
this.DataGrid.DataSource = dt;
this.DataGrid.DataMember = "customers";
declude 2003-07-14
  • 打赏
  • 举报
回复
dbGrid??是DataGrid吧?
see this:
namespace Microsoft.Samples.WinForms.Cs.Grid {
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using System.Data;
using System.Data.SqlClient;

public class Grid : System.Windows.Forms.Form {
private System.ComponentModel.Container components;
private System.Windows.Forms.StatusBar statusBar1;
private Microsoft.Samples.WinForms.Cs.Grid.Data.CustomersDataSet customersDataSet1;
private System.Windows.Forms.Button buttonLoad;
private System.Windows.Forms.DataGrid dataGrid1;

public Grid() {
// Windows 窗体设计器所必需的
InitializeComponent();
}

//处理 Load 按钮单击
//加载 Customers、Orders 和 OrderDetails 表并显示在网格中
private void buttonLoad_Click(object sender, System.EventArgs e) {
Cursor currentCursor = Cursor.Current;
try {
Cursor.Current = Cursors.WaitCursor;

//填充数据集
SqlConnection con = new SqlConnection("server=(local)\\NetSDK;Trusted_Connection=yes;database=northwind");
SqlDataAdapter cmdCustomers = new SqlDataAdapter("Select * from Customers", con);
SqlDataAdapter cmdOrders = new SqlDataAdapter("Select * from Orders", con);
SqlDataAdapter cmdOrderDetails = new SqlDataAdapter("Select * from [Order Details]", con);
statusBar1.Text ="正在加载客户...";
cmdCustomers.Fill(customersDataSet1, "Customers");
statusBar1.Text ="正在加载订单...";
cmdOrders.Fill(customersDataSet1, "Orders");
statusBar1.Text ="正在加载订单详细信息...";
cmdOrderDetails.Fill(customersDataSet1, "Order_Details");
statusBar1.Text ="正在更新网格...";
} finally {
statusBar1.Text ="完成";
Cursor.Current = currentCursor;
}
}


protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}

private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.customersDataSet1 = new Microsoft.Samples.WinForms.Cs.Grid.Data.CustomersDataSet();
this.buttonLoad = new System.Windows.Forms.Button();

dataGrid1.BeginInit();

if ( dataGrid1.TableStyles.Count == 0 ) {
dataGrid1.TableStyles.Add(new DataGridTableStyle());
}

this.dataGrid1.Text = "dataGrid1";
this.dataGrid1.TableStyles[0].PreferredRowHeight = 16;
this.dataGrid1.TableStyles[0].AlternatingBackColor = System.Drawing.Color.WhiteSmoke;
this.dataGrid1.Size = new System.Drawing.Size(584, 336);
this.dataGrid1.DataSource = customersDataSet1;
this.dataGrid1.DataMember = "Customers";
this.dataGrid1.ForeColor = System.Drawing.Color.Navy;
this.dataGrid1.TabIndex = 0;
this.dataGrid1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
this.dataGrid1.Location = new System.Drawing.Point(8, 8);
this.dataGrid1.BackColor = System.Drawing.Color.Gainsboro;

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.Text = "客户详细信息";
this.AcceptButton = buttonLoad;
this.ClientSize = new System.Drawing.Size(600, 413);

this.statusBar1.BackColor = System.Drawing.SystemColors.Control;
this.statusBar1.Location = new System.Drawing.Point(0, 397);
this.statusBar1.Size = new System.Drawing.Size(600, 16);
this.statusBar1.TabIndex = 2;
this.statusBar1.Text = "单击“加载”";

this.customersDataSet1.DataSetName = "CustomersDataSet";

this.buttonLoad.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
this.buttonLoad.Click += new System.EventHandler(buttonLoad_Click);
this.buttonLoad.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonLoad.Location = new System.Drawing.Point(480, 352);
this.buttonLoad.Size = new System.Drawing.Size(112, 32);
this.buttonLoad.TabIndex = 1;
this.buttonLoad.Text = "加载(&L)";
this.Controls.AddRange(new System.Windows.Forms.Control[] {statusBar1,
buttonLoad,
dataGrid1});
this.dataGrid1.EndInit();
}

[STAThread]
public static void Main(string[] args) {
Application.Run(new Grid());
}

}
}




110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧