DataGridView数据更新~
我的思想是将DataGridView中的数据弄到一个表中,然后再更新,但是现在只能用SqlCommand来实现,不能用update函数吗?试了好多方法都不行啊~strin...
我的思想是将DataGridView中的数据弄到一个表中,然后再更新,但是现在只能用SqlCommand来实现,不能用update函数吗?试了好多方法都不行啊~
string str = "Data Source=localhost;Initial Catalog=pd;user=sa;password=;";
SqlConnection myconnection = new SqlConnection(str);
myconnection.Open();
DataColumn[] keys = new DataColumn[18];
DataSet ds = new DataSet();
DataTable dt = new DataTable();
keys[0] = dt.Columns.Add("Test_number", typeof(string));
。。。。。
keys[17] = dt.Columns.Add("Test_level", typeof(string));
dt.PrimaryKey = keys;
ds.Tables.Add(dt);
string a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17;
a0 = "";
for (int i = 0; i < display.Rows.Count - 1; i++)
{
a0 = display.Rows[i].Cells[0].Value.ToString();
dt.Rows.Add(new object[] {a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17});
}
string mysql = "select * from Test";
SqlDataAdapter sa = new SqlDataAdapter(mysql, myconnection);
SqlCommandBuilder sb = new SqlCommandBuilder(sa);
sa.UpdateCommand = sb.GetUpdateCommand();
sa.Update(ds.Tables[0]);
这么写有什么问题吗?
不太懂下面的代码,我就想把一次把表里的数据给更新到数据库中~ 展开
string str = "Data Source=localhost;Initial Catalog=pd;user=sa;password=;";
SqlConnection myconnection = new SqlConnection(str);
myconnection.Open();
DataColumn[] keys = new DataColumn[18];
DataSet ds = new DataSet();
DataTable dt = new DataTable();
keys[0] = dt.Columns.Add("Test_number", typeof(string));
。。。。。
keys[17] = dt.Columns.Add("Test_level", typeof(string));
dt.PrimaryKey = keys;
ds.Tables.Add(dt);
string a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17;
a0 = "";
for (int i = 0; i < display.Rows.Count - 1; i++)
{
a0 = display.Rows[i].Cells[0].Value.ToString();
dt.Rows.Add(new object[] {a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17});
}
string mysql = "select * from Test";
SqlDataAdapter sa = new SqlDataAdapter(mysql, myconnection);
SqlCommandBuilder sb = new SqlCommandBuilder(sa);
sa.UpdateCommand = sb.GetUpdateCommand();
sa.Update(ds.Tables[0]);
这么写有什么问题吗?
不太懂下面的代码,我就想把一次把表里的数据给更新到数据库中~ 展开
1个回答
展开全部
你的写法有点问题,我贴一个你参考一下:
string connectionString = string.Empty;
string id = string.Empty;
string newValue = string.Empty;
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
try
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("SELECT * FROM jiuye WHERE xuehao = @ID", connection);
adapter.SelectCommand.Parameters.Add("@ID", SqlDbType.Int, 4, ID);
adapter.SelectCommand.Parameters[0].Value = id;
adapter.UpdateCommand = new SqlCommand("UPDATE jiuye SET jiuyebiaozhiid = @Value WHERE xuehao = @ID", connection);
adapter.UpdateCommand.Parameters.Add(new SqlParameter("@Value", SqlDbType.VarChar, 50, "jiuyebiaozhiid"));
adapter.UpdateCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, "xuehao"));
DataSet ds = new DataSet();
adapter.Fill(ds);
if(ds.Tables[0].Rows.Count > 0)
{
ds.Tables[0].Rows[0]["jiuyebiaozhiid"] = newValue;
adapter.Update(ds);
}
}
finally
{
connection.Close();
}
string connectionString = string.Empty;
string id = string.Empty;
string newValue = string.Empty;
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
try
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("SELECT * FROM jiuye WHERE xuehao = @ID", connection);
adapter.SelectCommand.Parameters.Add("@ID", SqlDbType.Int, 4, ID);
adapter.SelectCommand.Parameters[0].Value = id;
adapter.UpdateCommand = new SqlCommand("UPDATE jiuye SET jiuyebiaozhiid = @Value WHERE xuehao = @ID", connection);
adapter.UpdateCommand.Parameters.Add(new SqlParameter("@Value", SqlDbType.VarChar, 50, "jiuyebiaozhiid"));
adapter.UpdateCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, "xuehao"));
DataSet ds = new DataSet();
adapter.Fill(ds);
if(ds.Tables[0].Rows.Count > 0)
{
ds.Tables[0].Rows[0]["jiuyebiaozhiid"] = newValue;
adapter.Update(ds);
}
}
finally
{
connection.Close();
}
参考资料: http://baike.baidu.com/view/2814849
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询