数据库结构:
[CodeZZB] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[InterName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[Guid] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL CONSTRAINT [DF_LoginPerson_Guid] DEFAULT (newid()),
CONSTRAINT [PK_Test_Base] PRIMARY KEY CLUSTERED
(
[CodeZZB]
) ON [PRIMARY]
) ON [PRIMARY]
GO
定义 全局DataSet
{
//
string str_Conn="workstation id=JHTCHINA;packet size=4096;user id=sa;initial catalog=master;persist security info=False";
try
{
SqlConnection objConn=new SqlConnection(str_Conn);
string str_sql="select * from Test_Base";
SqlCommand objComm=new SqlCommand(str_sql,objConn);
SqlDataAdapter objAdapter=new SqlDataAdapter(objComm);
objAdapter.Fill(ds,"Test_Base");
dataGrid1.DataSource=ds;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
编辑添加dataGrid以后更新数据
{
try
{
//这里ds.Table[0]里面的数据已经改变
string str_Conn="workstation id=JHTCHINA;packet size=4096;user id=sa;initial catalog=master;persist security info=False";
//整体把修改的数据更新到数据库里面
SqlConnection objConn=new SqlConnection(str_Conn);
string str_sql="select * from Test_Base";
SqlCommand objComm=new SqlCommand(str_sql,objConn);
SqlDataAdapter objAdapter=new SqlDataAdapter(objComm);
SqlCommandBuilder updataBulid=new SqlCommandBuilder(objAdapter);
objAdapter.Update(ds,"Test_Base");
MessageBox.Show("OK");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
运行通过
如果是整体添加,在数据读取的时候
一句代码就可以了