2016-11-10 188 views
2

我已经尝试过几乎所有的解决方案,但我无法解决这个问题。我有通过ODBC连接从数据库中检索的数据。数据在那里。它将进入数据网格视图,但我无法获取这些数据进入我的本地SQL数据库。请告诉我我做错了什么。C#从数据表插入数据到SQL Server数据库

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) 

就像我说的没有错误出现。数据只是不保存到数据库。

+0

您需要通过线通过您的代码行的方式执行,并观察其运行。然后您需要使用SQL Profiler来观察正在执行的查询 –

+0

由于您没有得到错误和验证执行!你正在检查正确的数据库和表插入? – Adil

回答

1
"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,因为它是钱。

0

我发现您的代码没有错误。我发现mdf文件的连接定义是错误的。

|DataDirectory|设置路径到应用程序运行的文件夹。在这种情况下,如果我们以调试模式运行,它将在Debug \ bin文件夹中创建单独的应用程序exe,其中包含应用程序资源,如.mdf文件。或在发布模式下,它将在发布文件夹内创建特定的文件夹。因此,您需要更改数据库连接字符串的数据库文件名,或者需要为连接字符串提供完整的目录路径。例如

tempDbConnection.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TempDB.mdf;Integrated Security=True"; 
} 

更换

tempDbConnection.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Promod\Documents\Visual Studio 2012\Projects\Contribution1\Contribution1\bin\Debug\TempDB.mdf;Integrated Security=True"; 
相关问题