简体   繁体   中英

DataSet Insert into tables

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

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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM