2 u013363255 u013363255 于 2015.07.26 23:38 提问

DataSet自动生成updatacommand更新数据库出错的问题

我已经设置了主键了,而且是自增的保证唯一,程序里面也再次设置了一次如:
DataColumn[] dcs = { dt.Columns["ID"] };
dt.PrimaryKey = dcs;

            但是,TM就是出错啊出错,那个“  对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。”错误不散就是不散,我不想去写SQL那句了,不是懒,就是拼凑的方法真心太low了,还要考虑那么多IF,不如Dataset那句dt.updata();来得直接啊



        完整代码在这里,sqlparameter可以参数化,但是如果每次修改的列不同怎么办,这次修改了姓名,下次可能只修改身份证号,还是拼接啊

string SQL_Refresh = "select * from 报名表 where ID='"+ ID.ToString()+"'";
SqlCommand cmd = new SqlCommand(SQL_Refresh, cnn);
DataSet Data_all = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
SqlCommandBuilder scb = new SqlCommandBuilder(adp);
adp.Fill(Data_all, "报名表");

        DataTable dt = Data_all.Tables["报名表"];
        //foreach (DataRow row in dt.Rows)
        //{
        //    string a= row["ID"].ToString();
        //    string b= row["姓名"].ToString();
        //    string c= row["证件号码"].ToString();
        //   string d=row["民族"].ToString();

        //}

        if (Data_all == null)
        {
            return;
        }

        //DataColumn[] dcs = { dt.Columns["ID"] };
        //dt.PrimaryKey = dcs;
        //DataRow dr = dt.Rows.Find(ID.ToString());
        DataColumn[] dcs = { dt.Columns["ID"] };
        dt.PrimaryKey = dcs;
        DataRow[] drs = dt.Select("ID=" +ID.ToString());
        DataRow dr = drs[0];

        dr.BeginEdit();
        if (StudentName.Text != _StudentName)
        {
            dr["姓名"] = StudentName.Text;
            for (int i = 0; i < 4; i++)
            {
                if (File.Exists(path_Pre + S[i] + ".tif"))
                {
                    Computer MyComputer = new Computer();
                    MyComputer.FileSystem.RenameFile(path_Pre + S[i] + ".tif", path_Real + S[i] + ".tif");
                }
            }
        }



        dr["性别"] = Sex.Text;
        dr["出生日期"] = BornDate.Text;
        dr["户籍性质"] = Citizenkind.Text;
        dr["民族"] = Nation.Text;
        dr["毕业学校"] = School.Text;

        if (DetailSite.Text != "")
        {
            dr["行政区划代码"] = SiteCode;
            dr["联系地址"] = FullSite.Substring(Province.Text.Length + Shiji.Text.Length);
            dr["完全详细地址"] = FullSite;
            dr["证件号码"] = Identify.Text;
            dr["省级名称"] = Province.Text;
            dr["地州级名称"] = Shiji.Text;
            dr["县级名称"] = Town.Text;
        }
        dr["家长电话"] = ParentsPhone.Text;
        if (Selfphone.Text != "")
        {
            dr["学生电话"] = Selfphone.Text;
        }
        dr["家长姓名"] = Parents.Text;
        dr["所报专业代码"] = Major.Text;
        //dr["报名时间"] = dateTimePicker1.Text;
        dr["书费"] = Bookcost.Text;
        dr["生活费"] = livecost.Text;

        if (File.Exists(paths_fore + paths_or + "_sfz_A.tif"))
        {
            dr["有无身份证正面"] = "1";
        }
        else
        {
            dr["有无身份证正面"] = "0";
        }
        if (File.Exists(paths_fore + paths_or + "_sfz_B.tif"))
        {
            dr["有无身份证背面"] = "1";
        }
        else
        {
            dr["有无身份证背面"] = "0";
        }

        if (File.Exists(paths_fore + paths_or + "_hk_A.tif"))
        {
            dr["有无户口首页"] = "1";
        }
        else
        {
            dr["有无户口首页"] = "0";
        }
        if (File.Exists(paths_fore + paths_or + "_hk_B.tif"))
        {
            dr["有无户口本人页"] = "1";
        }
        else
        {
            dr["有无户口本人页"] = "0";
        }
        if (File.Exists(paths_fore + paths_or + "_pkzm.tif"))
        {
            dr["有无贫困证明"] = "1";
        }
        else
        {
            dr["有无贫困证明"] = "0";
        }

       dr.EndEdit();
        try
        {
            adp.Update(Data_all, "报名表");
            Data_all.Clear();
        }
        catch (SqlException exp)
        {
            MessageBox.Show(exp.Message.ToString());
        }

2个回答

caozhy
caozhy   Ds   Rxr 2015.07.27 00:06

要看完整的代码,另外,你可以用参数化sql
sqlparameter,不需要拼接。

u013363255
u013363255 sqlparameter和拼接SQL命令不是差不多的么……还要想办法换逗号……
2 年多之前 回复
CSDNXIAON
CSDNXIAON   2015.07.28 04:56

dataset更新数据的问题
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片