DataSet插入表中

时间:2011-10-22 21:20:11

标签: c# sql insert dataset

是否可以使用DataSet输入插入SQL?

我已经创建了我的DataSet

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

现在我想将数据插入到SQL表xx中,我该怎么做?

提前谢谢你

1 个答案:

答案 0 :(得分:1)

您可以尝试这样做......不使用数据集

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