将 DataTable 保存到数据库表中

发布于 2024-11-09 12:22:36 字数 1166 浏览 0 评论 0原文

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

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

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

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

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'。”

I am generating a Data Table from my application and I would like to save the whole Data Table into one database table.

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

The DataTable is getting generated but What to do next.Show me some syntax.I dont really need the select statement there either, i just want to insert all the info from the DataTable into already created db table(update the table).
I will use the ODBC connection to access the MYSQL database

i want to update the data into the database through dataset directly

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 { }
}

It will be used like this method catch this exception
"Update unable to find TableMapping['Table'] or DataTable 'Table'."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂牵梦绕锁你心扉 2024-11-16 12:22:36

您应该创建插入 sql 字符串,循环遍历数据表中的所有行,为数据表中的每一列添加参数,然后执行此查询。

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

You should create insert sql string, loop thorugh all rows in your datatable, add parameters for each column in datatable, and execute this query.

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();   
   }
}
拧巴小姐 2024-11-16 12:22:36
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();
  }
}
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();
  }
}
溺深海 2024-11-16 12:22:36

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

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

You wrote: >i want without foreach loop

Try to use MySqlDataTable class in dotConnect for MySQL. Read more about the MySqlDataTable.Update() method and MySqlDataTable.CachedUpdates state.

心如狂蝶 2024-11-16 12:22:36
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);
        }
    }
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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文