2016-06-28 126 views
0

我已经完成了一个包含gridviewc#表单。我想将选定的行数据插入按钮单击的临时表。我已经尝试过这样但它不起作用。它显示了错误:如何创建一个sql临时表并使用sql命令向其中插入数据(C#)

String or Binary Data would be truncated. The statement has been terminated

下面

是我的代码: -

private void button1_Click(object sender, EventArgs e) 
    { 
     int i = 0; 
     List<int> ChkedRow = new List<int>(); 

     for (i = 0; i <= dataGridView1.RowCount - 1; i++) 
     { 
      if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column1"].Value) == true) 
      { 
       ChkedRow.Add(i); 
      } 
     } 

     if (ChkedRow.Count == 0) 
     { 
      MessageBox.Show("Select Items to view report"); 
      return; 
     } 

     foreach (int j in ChkedRow) 
     { 
      String ConnectionString1 = @"Data Source=.\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=True;Pooling=False"; 

      cnnStr = @"create table ##gridtemp (itmcod varchar(7)) INSERT INTO ##gridtemp (itmcod) 
         VALUES ('" + dataGridView1.Rows[j].Cells["title"].Value.ToString() + "');"; 

      try 
      { 
       using (SqlConnection cs = new SqlConnection(ConnectionString1)) 
       { 

        using (cmd = new SqlCommand(cnnStr, cs)) 
        { 
         cs.Open(); 
         cmd.ExecuteNonQuery(); 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
     } 
     MessageBox.Show("Records Added Succesfully"); 
    } 
+0

你为什么要在一个临时表中插入? – Shaharyar

+0

主表中包含大量的记录。我只需要过滤的记录报告 –

+0

错误描述非常清楚:'dataGridView1.Rows [j] .Cells [“title”]。Value.ToString()'是一个大于7个字符的字符串。 –

回答

0
private void button1_Click(object sender, EventArgs e) 
    { 
     SqlConnection conn1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=tempdb;Integrated Security=True"); 
     SqlCommand cmd1 = new SqlCommand("create table ##gridtemp (itmcod varchar(MAX))", conn1); 
     conn1.Open(); 
     cmd1.ExecuteNonQuery(); 
     DataTable dts = new DataTable(); 
     SqlBulkCopy bulkCopy = new SqlBulkCopy(conn1); 
     bulkCopy.DestinationTableName = "##gridtemp"; 
     bulkCopy.WriteToServer(dts); 
     conn1.Close(); 

     int i = 0; 
     List<int> ChkedRow = new List<int>(); 

     for (i = 0; i <= dataGridView1.RowCount - 1; i++) 
     { 
      if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column1"].Value) == true) 
      { 
       ChkedRow.Add(i); 
      } 
     } 

     if (ChkedRow.Count == 0) 
     { 
      MessageBox.Show("Select Items to view report"); 
      return; 
     } 

     foreach (int j in ChkedRow) 
     { 
      String ConnectionString1 = @"Data Source=.\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=True;Pooling=False"; 

      cnnStr = @"INSERT INTO ##gridtemp (itmcod) 
         VALUES ('" + dataGridView1.Rows[j].Cells["itmcod"].Value.ToString() + "');"; 

      try 
      { 
       using (SqlConnection cs = new SqlConnection(ConnectionString1)) 
       { 

        using (cmd = new SqlCommand(cnnStr, cs)) 
        { 
         cs.Open(); 
         cmd.ExecuteNonQuery(); 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
     } 
     MessageBox.Show("Records Added Succesfully"); 
    } 
} 
相关问题