Reputation: 4261
Is that possible to insert into SQL with DataSet
input ?
I have created my DataSet
like this :
SqlCommand comm_SelectAll = new SqlCommand(sql_SelectAll, connectionWrapper.conn);
comm_SelectAll.Parameters.AddWithValue("@NO_CLIENT", IdClient);
if (Anne != "")
comm_SelectAll.Parameters.AddWithValue("@DATE_PERIMER", Anne);
SqlDataAdapter adapt_SelectAll = new SqlDataAdapter();
adapt_SelectAll.SelectCommand = comm_SelectAll;
DataSet dSet_SelectAll = new DataSet();
adapt_SelectAll.Fill(dSet_SelectAll);
dSet_SelectAll.Dispose();
adapt_SelectAll.Dispose();
Now I want to insert data into SQL table xx, how can I do that ?
Thanks you in advance
Upvotes: 0
Views: 2638
Reputation: 17701
You can try like this ...with out using dataset
public static string BuildSqlNativeConnStr(string server, string database)
{
return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", server, database);
}
private void simpleButton1_Click(object sender, EventArgs e)
{
const string query = "Insert Into Employees (RepNumber, HireDate) Values (@RepNumber, @HireDate)";
string connStr = BuildSqlNativeConnStr("apex2006sql", "Leather");
try
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.Add(new SqlParameter("@RepNumber", 50));
cmd.Parameters.Add(new SqlParameter("@HireDate", DateTime.Today));
cmd.ExecuteNonQuery();
}
}
}
catch (SqlException)
{
System.Diagnostics.Debugger.Break();
}
}
Upvotes: 1