C#将数据从数据表插入SQL Server数据库

斯科特骑术

我试图接近这个网站上每一个解决方案,但我解决不了这个问题。我有通过ODBC连接从数据库中检索到的数据。该数据是存在的。它将很好地进入“数据网格视图”,但是我无法获取这些数据进入本地SQL数据库。请告诉我我在做什么错。

    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)

就像我说的那样,没有错误。数据只是不会保存到数据库中。

莫希特·什里瓦斯塔瓦(Mohit Shrivastava)
"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和ines之间的'',因为它是整数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

c# 将数据库提取到 SQL Server 数据表中

如何从数据表vb.net向SQL Server数据库插入值

C#无法将数据插入SQL Server数据库

使用C#插入SQL Server数据库的问题

批量插入SQL Server 2012数据库-C#

实体框架 - 将记录列表从数据表保存到 SQL Server 数据库表

如何将数据插入SQL Server数据库

C#-将包含关系数据表的数据集插入到SQL Server中

将数据从SQL Server数据库导入HTML表

将C#数据表批量插入到SQL Server-有效检查重复项

无法从C#将inkPicture插入SQL Server数据库

将大数据表写入mysql数据库C#

IN SQL Server数据库

使用实体框架将数据表数据插入数据库?

数据未从 C# 组合框插入 SQL Server 数据库

将语句插入SQL Server数据库

将空值插入SQL Server数据库

将 XML 文件插入 SQL Server 中的数据库

使用PDO将映像插入SQL Server数据库

不将数据表单插入数据库表PHP

使用selectInput更新数据表并插入数据库

SQL server 的数据库是 sql 数据库吗?

Xamarin/C# - 为多个表以编程方式将数据插入 SQLite 数据库?

Select语句从Netezza数据库中获取数据并插入SQL Server数据库(SSIS)

如何将数据表值插入数据库

使用SqlbulkcopycolumnMapping将数据从数据表复制到sql数据库

将数据从一个数据库表插入到SQL Server中的另一数据库表

从本地 SQL Server 数据库和 C# 同步实时数据库

无法使用C#将数据导入SQL Server数据库