J
James Shen
I have the following code which uses SqlCommand, sqlCommandBuilder and
SqlDataAdapter. According to the doc (MSDN), Update will use the
command builder to build SQL.
However, debugger shows UpdateCommand is null.
This code updates the db correctly (after removing the toString line).
test table has two columns (id int not null, value nvarchar(20)).
Any ideas?
Thanks in advance
James
[WebMethod]
public DataSet TestSqlCmd()
{
string myConnectString = "user id=sa;password=sa;initial
catalog=playground;data source=localhost;Connect Timeout=30";
try
{
SqlConnection conn = new SqlConnection(myConnectString);
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * from test", conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
adapter.Fill(dt);
ds.Tables.Add(dt);
SqlCommandBuilder b = new SqlCommandBuilder(adapter);
dt.Rows[0]["value"] = "x";
adapter.Update(dt);
SqlCommand uc = adapter.UpdateCommand;
string st = uc.CommandText;
return ds;
}
catch(Exception ex)
{
string s = ex.ToString();
}
return null;
}
SqlDataAdapter. According to the doc (MSDN), Update will use the
command builder to build SQL.
However, debugger shows UpdateCommand is null.
This code updates the db correctly (after removing the toString line).
test table has two columns (id int not null, value nvarchar(20)).
Any ideas?
Thanks in advance
James
[WebMethod]
public DataSet TestSqlCmd()
{
string myConnectString = "user id=sa;password=sa;initial
catalog=playground;data source=localhost;Connect Timeout=30";
try
{
SqlConnection conn = new SqlConnection(myConnectString);
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * from test", conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
adapter.Fill(dt);
ds.Tables.Add(dt);
SqlCommandBuilder b = new SqlCommandBuilder(adapter);
dt.Rows[0]["value"] = "x";
adapter.Update(dt);
SqlCommand uc = adapter.UpdateCommand;
string st = uc.CommandText;
return ds;
}
catch(Exception ex)
{
string s = ex.ToString();
}
return null;
}