DataAdapter.Update未更新

时间:2016-06-27 09:51:18

标签: sql sqldataadapter

嘿希望有人可以帮助,发疯: - )

做一个获取数据集并再次更新它的程序,我有以下SQLclass。数据集遍历正常,值已更改,但是dosnt将提交到DB。

class SQLConnection
{
    private string SQL_String;
    private string strCON;
    System.Data.SqlClient.SqlDataAdapter da_1;

    public string Sql {
        set { SQL_String = value; }
    }

    public string connection_string
    {
        set { strCON = value; }      

    }

    public System.Data.DataSet Getconnection
    {
        get { return Getmydataset(); }

    }

    private System.Data.DataSet Getmydataset()
    {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCON);

        con.Open();

        da_1 = new System.Data.SqlClient.SqlDataAdapter(SQL_String, con);

        System.Data.DataSet dat_set = new System.Data.DataSet();
        da_1.Fill(dat_set, "Table_Data_1");



        return dat_set;

    }

    public void UpdateDatabase(System.Data.DataSet ds)
    {


        System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1);

        int test = cb.DataAdapter.Update(ds.Tables[0]);

    }

}
}

当我调用该类并尝试更新时,DataAdapter返回0

static public void Main(string[] args)
    {
                GetClosedComputers();
    }

getclosedcomputers

public static DataSet GetClosedComputers()
    {
        try
        {
            SQLConnection objConnect = new SQLConnection();
            conString = Properties.Settings.Default.Connectionstring;

            objConnect.connection_string = conString;
            //call stored procedure
            objConnect.Sql = Properties.Settings.Default.GetNextClosedComputers;


            ds = objConnect.Getconnection;


            ds.Tables[0].Columns["State"].Expression = "'1'";
            objConnect.UpdateDatabase(ds);

        }
        catch (Exception e)
        {
            ErrorString = e.Message;

        }
        return ds;
    }

1 个答案:

答案 0 :(得分:0)

嗯,好像我不得不问题,首先我不能使用UpdateProcedure,我不知道为什么以及" .Expression"可以使用,这解决了它。

objConnect = new SQLConnection();
conString = Properties.Settings.Default.Connectionstring;

objConnect.connection_string = conString;
// use sql query instead of stored procedure!!!
objConnect.Sql = Properties.Settings.Default.Test;

//Dataset with all computers
ds = objConnect.Getconnection;

//DataSet results = StartStopVM.StartVirtualMachines(ds);
DataTable dt = ds.Tables[0];

//this didnt work
//ds.Tables[0].Columns["State"].Expression = "'1'";
//
foreach(DataRow dr in dt.Rows)
{
   dr["State"] = 1;

}
objConnect.UpdateDatabase(ds);