C#操作sql数据库,更新数据,如何只更新其中的几项
如题:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void btnupdata_Click(object sender, EventArgs e)
{
try
{
string id = textBox1.Text;
string name = textBox2.Text;
string sex = textBox3.Text;
string sal = textBox4.Text;
string birth = textBox5.Text;
string str = "Server= LYP19900208;DataBase=test01;Uid=sa;Pwd=6020";
SqlConnection conn = null;
conn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "update TB_EMP set empName = '" + name + "' , sex = '" + sex + "' ,sal = '" + sal + "' , brithday = '" + birth + "' where empID = '" + id + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds, "TB_EMP");
MessageBox.Show("更新数据成功");
}
catch
{
MessageBox.Show("更新数据失败");
}
}
}
}
以上是我编的代码,其中如果根据empID更新数据的话必须全都改,如果有一项不输入数据的话更新完了就是空的,求教怎么能只更新其中的一项或者两项,新手上路,请多多关照= = --------------------编程问答-------------------- --------------------编程问答-------------------- string sql = "update TB_EMP set";
if(name!="")
sql+=" empName='" + name + "'";
其他字段相同处理方法 --------------------编程问答-------------------- string sql = "update TB_EMP set";
if(name!="")
sql+=" empName='" + name + "'";
其他字段相同处理方法
+1 --------------------编程问答-------------------- 如果通过empid分不开的话,就别用update了,在表格中修改,完后提交到数据库
adapter有Update方法 --------------------编程问答-------------------- 如果你是做编辑的话,那你进入编辑页面的时候就应该把这条记录提取显示在编辑页面,就不会出现你说的空值了. --------------------编程问答-------------------- +1;;;'''、、、
补充:.NET技术 , C#