SqlCommand
using
(SqlConnection conn
=
new
SqlConnection(
"
server=127.0.0.1;database=shui
"
+
"
;uid=
"
+
txt_uid.Text.Trim()
+
"
;pwd=
"
+
txt_pwd.Text.Trim())

...
{
conn.Open();
using(SqlCommand cmd = conn.CreateCommand())

...{
cmd.CommandText = string.Format("UPDATE yhb SET zs = '{1}' WHERE zcm='{0}'",
txt_zcm.Text, txt_zc.Text); // 此处放置你的 SQL 语句
cmd.ExecuteNonQuery();
}
}
SqlParameter
SqlConnection conn
=
new
SqlConnection(
"
server=127.0.0.1;database=shui
"
+
"
;uid=
"
+
txt_uid.Text.Trim()
+
"
;pwd=
"
+
txt_pwd.Text.Trim());
conn.Open();
SqlCommand STcmd
=
new
SqlCommand(conn);
STcmd.CommandText
=
"
update yhb set zs = @txt_zs where zcm = @zcm
"
;
SqlParameter p1
=
new
SqlParameter(
"
@txt_zs
"
);
p1.Value
=
txt_zs.Text;
SqlParameter p2
=
new
SqlParameter(
"
@zcm
"
);
p2.Value
=
"
100001
"
;
STcmd.Parameters.Add(p1);
STcmd.Parameters.Add(p2);
cmd.ExecuteNonQuery();
SqlCommand.Parameters.AddWithValue
SqlConnection conn
=
new
SqlConnection(
"
server=127.0.0.1;database=shui
"
+
"
;uid=
"
+
txt_uid.Text.Trim()
+
"
;pwd=
"
+
txt_pwd.Text.Trim());
SqlCommand STcmd
=
new
SqlCommand(
"
update yhb set zs = @zs where zcm = '100001'
"
, conn);
STcmd.Parameters.AddWithValue(
"
@zs
"
,txt_zs.Text);
conn.Open();
STcmd.ExecuteNonQuery();
conn.Close();
StringBuilder
StringBuilder strBuilder
=
new
StringBuilder(
2000
);
strBuilder.AppendFormat(
"
UPDATE yhb SET zs = '{1}' WHERE zcm='{0}'
"
,
txt_zcm.Text, txt_zc.Text);
System.String strSql
=
strBuilder.toString();
using
(SqlConnection conn
=
new
SqlConnection(
"
server=127.0.0.1;database=shui
"
+
"
;uid=
"
+
txt_uid.Text.Trim()
+
"
;pwd=
"
+
txt_pwd.Text.Trim())
...
{
conn.Open();
using(SqlCommand cmd = conn.CreateCommand())
...{
cmd.CommandText = string.Format("UPDATE yhb SET zs = '{1}' WHERE zcm='{0}'",
txt_zcm.Text, txt_zc.Text); // 此处放置你的 SQL 语句
cmd.ExecuteNonQuery();
}
}
SqlConnection conn
=
new
SqlConnection(
"
server=127.0.0.1;database=shui
"
+
"
;uid=
"
+
txt_uid.Text.Trim()
+
"
;pwd=
"
+
txt_pwd.Text.Trim());
conn.Open();
SqlCommand STcmd
=
new
SqlCommand(conn);
STcmd.CommandText
=
"
update yhb set zs = @txt_zs where zcm = @zcm
"
;
SqlParameter p1
=
new
SqlParameter(
"
@txt_zs
"
);
p1.Value
=
txt_zs.Text;
SqlParameter p2
=
new
SqlParameter(
"
@zcm
"
);
p2.Value
=
"
100001
"
;
STcmd.Parameters.Add(p1);
STcmd.Parameters.Add(p2);
cmd.ExecuteNonQuery();
SqlConnection conn
=
new
SqlConnection(
"
server=127.0.0.1;database=shui
"
+
"
;uid=
"
+
txt_uid.Text.Trim()
+
"
;pwd=
"
+
txt_pwd.Text.Trim());
SqlCommand STcmd
=
new
SqlCommand(
"
update yhb set zs = @zs where zcm = '100001'
"
, conn);
STcmd.Parameters.AddWithValue(
"
@zs
"
,txt_zs.Text);
conn.Open();
STcmd.ExecuteNonQuery();
conn.Close();
StringBuilder strBuilder
=
new
StringBuilder(
2000
);
strBuilder.AppendFormat(
"
UPDATE yhb SET zs = '{1}' WHERE zcm='{0}'
"
,
txt_zcm.Text, txt_zc.Text);
System.String strSql
=
strBuilder.toString();

本文通过具体的C#代码示例展示了如何使用不同的方法来更新SQL数据库中的记录。包括直接设置SQL语句、使用SqlParameter参数化查询及StringBuilder构建SQL语句等方法。
204

被折叠的 条评论
为什么被折叠?



