将DataTable保存到数据库表中

时间:2011-05-26 05:52:32

标签: c# mysql .net datatable

我正在从我的应用程序生成数据表,我想将整个数据表保存到一个数据库表中。

DataTable ds = //add the info from the queue in the application

DataTable正在生成但是下一步做什么。给我一些语法。我真的不需要那里的select语句,我只想将DataTable中的所有信息插入到已创建的db表中(更新表) 。 我将使用ODBC连接来访问MYSQL数据库

我想直接通过数据集将数据更新到数据库

public void update(DataTable ds)
{
  try
  {  
    lock (myLockHolder)
    {
      X1 = 1;
      OdbcConnection con = 
      new OdbcConnection(LocalConnection.GetLocalConnetionString());
      OdbcCommand cmd;
      OdbcDataAdapter da;
      DataSet ds1=new DataSet();
      string query = "";
      query = "update parameter" + Environment.NewLine;
      query += "set paramvalue=paramvalue,date_logged1=date_logged1," 
        + Environment.NewLine;
      query += "  Quality=Quality,date_logged=date_logged" 
        + Environment.NewLine;
      query += " where itemID=itemID";
      cmd = new OdbcCommand(query, con);
      da = new OdbcDataAdapter(cmd);
      ds1=new DataSet();
      ds1.Tables.Add(ds);
      da.Update(ds1);                   

    }
  }
  catch { }
  finally { }
}

它将像这个方法一样使用捕获此异常 “更新无法找到TableMapping ['Table']或DataTable'Table'。”

4 个答案:

答案 0 :(得分:2)

您应该创建insert sql string,遍历数据表中的所有行,为datatable中的每一列添加参数,然后执行此查询。

string sql = "INSERT INTO T (A, B, C) VALUES (@A, @B, @C)"; 
using (SqlConnection conn = new SqlConnection(connectionString))
{   
   conn.Open();   
   foreach (DataRow r in myTable.Rows)   
   {      
       SqlCommand cmd = conn.CreateCommand();      
       cmd.CommandText = sql;      
       cmd.Parameters.AddWithValue("@A", r["A"]);      
       cmd.Parameters.AddWithValue("@B", r["B"]);      
       cmd.Parameters.AddWithValue("@C", r["C"]);      
       cmd.ExecuteNonQuery();   
   }
}

答案 1 :(得分:1)

using (SqlConnection conn = new SqlConnection(connectionString))
{   
  conn.Open();  
  foreach (DataRow r in dataTable2.Rows)
  {
    string sql = "INSERT INTO BALREP (ACODE, ANAME, QTY) VALUES ('" +r["CODE"] + "', '" + r["NAME"] + "', '" + r["CELL"] + "')";
    SqlCommand cmd2 = con.CreateCommand();
    cmd.CommandText = sql;
    cmd.ExecuteNonQuery();
  }
}

答案 2 :(得分:0)

您写道:> 我想要没有foreach循环

尝试在dotConnect for MySQL中使用MySqlDataTable类。阅读有关MySqlDataTable.Update()方法和MySqlDataTable.CachedUpdates状态的更多信息。

答案 3 :(得分:0)

dt.Rows.Clear();
        try { 
        SqlConnection con = new SqlConnection(ConnectDb.connectionstring);

        SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM Admission",con);
        con.Open();
        adp.Fill(dt);

        SqlConnection con2 = new SqlConnection(ConnectDb.onlinestring);
        con2.Open();


        foreach (DataRow r in dt.Rows)
        {
            SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM [Admission] WHERE ([studentName] = @SName)", con2);
            check_User_Name.Parameters.AddWithValue("@SName", r["studentName"]);
            int UserExist = (int)check_User_Name.ExecuteScalar();

            if (UserExist > 0)
            {
                //Username exist
                //MessageBox.Show("User Name is Already here");

            }
            else {
            SqlCommand cmd = new SqlCommand("INSERT INTO Admission(studentName, age, class, subject)VALUES(@name, @age, @class, @subject)", con2);
            cmd.Parameters.AddWithValue("@name", r["studentName"]);
            cmd.Parameters.AddWithValue("@age", r["age"]);
            cmd.Parameters.AddWithValue("@class", r["class"]);
            cmd.Parameters.AddWithValue("@subject", r["subject"]);
            cmd.ExecuteNonQuery(); 
        }

        }
            }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }