C#将数据从数据表插入SQL Server数据库 [英] C# insert data from datatable to SQL Server database

查看:73
本文介绍了C#将数据从数据表插入SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在该网站上尝试了几乎所有解决方案,但无法解决此问题.我有通过ODBC连接从数据库中检索到的数据.数据在那里.它将很好地进入数据网格视图",但是我无法将这些数据输入到本地SQL数据库中.请告诉我我在做什么错.

I have tried nearly every solution on this website, but I can't solve this problem. I have data that has been retrieved from a database through an ODBC connection. The data is there. It will go into a Data Grid View just fine, but I can't get this data to go into my local SQL database. Please tell me what I'm doing wrong.

    public partial class frmNorth : Form
{
        // variables for the connections 
        private OdbcConnection epnConnection = new OdbcConnection();
        private SqlConnection tempDbConnection = new SqlConnection();
public frmNorth()
{
    InitializeComponent();
    // This is for the ePN DB
    epnConnection.ConnectionString = @"Dsn=ePN; uid=username; pwd=myPa$$Word";
    // This is for the local DB
    tempDbConnection.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TempDB.mdf;Integrated Security=True";
}
private void btnLoadData_Click(object sender, EventArgs e)
{
    try
        {
            //===This part works just fine===============================================================
            epnConnection.Open();
            string epnQuery =   "SELECT FNCL_SPLIT_REC_ID, PROJ_ID, SALES_SRC_PRC " +
                                "FROM PROJ_FNCL_SPLIT " +
                                "WHERE PROJ_ID=" + textBox1.Text + "";
            OdbcCommand epnCommand = new OdbcCommand(epnQuery, epnConnection);
            epnCommand.CommandTimeout = 0;

            //This connects the data to the data table
            OdbcDataAdapter da = new OdbcDataAdapter(epnCommand);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            //===========================================================================================


            //======The part below is the part that wont work. The data wont go into the SQL database====
            tempDbConnection.Open();
            string tempSql = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                tempSql =   "INSERT INTO tblTemp (FNCL_SPLIT_REC_ID, PROJ_ID, SALES_SRC_PRC) VALUES ('"
                            + dt.Rows[i]["FNCL_SPLIT_REC_ID"].ToString().Trim() + "','"
                            + dt.Rows[i]["PROJ_ID"].ToString().Trim() + "','"
                            + dt.Rows[i]["SALES_SRC_PRC"].ToString().Trim() + "');";
                SqlCommand tempCommand = new SqlCommand(tempSql, tempDbConnection);
                tempCommand.ExecuteNonQuery();
            }
                // There are no errors. The data just doesn't save to the database.
            //===========================================================================================

            epnConnection.Close();
            tempDbConnection.Close();

        }
        catch (Exception ex)
        {
            epnConnection.Close();
            tempDbConnection.Close();
            MessageBox.Show("Error " + ex);
        }
    }
}
}

    //+++++++++++++++++++This is what the table looks like+++++++++++++++++++++++++++++++++++++++++++++++
    CREATE TABLE [dbo].[tblTemp] (
[FNCL_SPLIT_REC_ID] INT        NOT NULL,
[PROJ_ID]           NCHAR (10) NULL,
[SALES_SRC_PRC]     MONEY      NULL,
PRIMARY KEY CLUSTERED ([FNCL_SPLIT_REC_ID] ASC)

就像我说的那样,不会出现任何错误.数据只是不会保存到数据库中.

Like I said no errors come up. The data just doesn't save to the database.

推荐答案

"INSERT INTO tblTemp (FNCL_SPLIT_REC_ID, PROJ_ID, SALES_SRC_PRC) VALUES ("
                    + dt.Rows[i]["FNCL_SPLIT_REC_ID"].ToString().Trim() + ",'"
                    + dt.Rows[i]["PROJ_ID"].ToString().Trim() + "',"
                    + dt.Rows[i]["SALES_SRC_PRC"].ToString().Trim() + ");";

删除了FNCL_SPLIT_REC_ID为int和SALES_SRC_PRC之间的',因为它是货币.

Removed the ' ' between FNCL_SPLIT_REC_ID as it is int and SALES_SRC_PRC since it is money.

这篇关于C#将数据从数据表插入SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆