2016-11-05 72 views
0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace crudwithlogin 
{ 
    public partial class student_registration : Form 
    { 
     SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Android\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30"); 

     public student_registration() 
     { 
      InitializeComponent(); 
     } 

     private void student_registration_Load(object sender, EventArgs e) 
     { 
      label1.Text = "You are logged in as "+((Form)this.MdiParent).Controls["label1"].Text; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      conn.Open(); 
      SqlCommand cmd = conn.CreateCommand(); 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "insert into student values ('" 
                + textBox1.Text 
                + "','" 
                + textBox2.Text 
                + "''" 
                + dateTimePicker2.Value.ToString() 
                + "','" 
                + textBox4.Text 
                + "','" 
                + comboBox2.Text 
                + "','" 
                + textBox6.Text 
                + "','" 
                + dateTimePicker1.Value.ToString() 
                + "')"; 
      cmd.ExecuteNonQuery(); 
      conn.Close(); 
      MessageBox.Show("Data Added Successfully. "); 
     } 
    } 
} 

专家您好,我创建一个使用C#的Windows窗体应用程序,我想 数据插入到数据库中,但我得到的值的误差和列不匹配,即使我正在为正确的属性输入正确的值。C#.NET的WinForms数据插入错误

我有8个属性(ID与自动增量),并为剩余的7列我输入7个值,但我得到错误值不匹配。

请帮我解决这个问题。我重视

错误我得到的是这里显示的代码和问题截图:

enter image description here

数据库模式截图在这里

enter image description here

+0

请让我知道如果你的问题还没有解决 – Plycoder

+1

可能重复的[sql server:列名或提供的值数量不匹配表定义](http://stackoverflow.com/questions/20372361/sql- server-column-name-or-number-of-supplied-values-does-not-match-table-defini) – Shell

+0

[SQL注入警报](http://msdn.microsoft.com/zh-cn/library/ms161953 %28v = sql.105%29.aspx) - 你应该不**连接你的SQL语句 - 使用**参数化查询**来代替以避免SQL注入 –

回答

1
('" + textBox1.Text + "','" + textBox2.Text + "''" + dateTimePicker2.Value.ToString() + "','" 
//missed , ------------------------------------^ 

在未来也使用Command.Parameters,你将避免这样的问题!您也将受到保护SqlInjection。这里举例说明如何定义你的参数。

SqlCommand cmd = conn.CreateCommand(); 
cmd.CommandType = CommandType.Text; 
cmd.CommandText = @"insert into TableNameFromDB values (@ID, @Title)"; 
cmd.Parameters.AddWithValue("@ID", yourIDValue); 
cmd.Parameters.AddWithValue("@Title", yourTitleValue); 
cmd.ExecuteNonQuery(); 
conn.Close(); 

也不要在您的应用程序中共享一个连接,使用多个连接。连接池是你的朋友。检查什么是使用块,并用它来自动处理您的IDisposable对象(例如:SqlConnection)。

+0

我会写'yourIDValue'的地方吗? textbox1.text? –

0

仔细看你的插入查询有一个,(昏迷)失踪。请你正确地修改你的查询,如下所示:

"insert into student values ('" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker2.Value.ToString() + "','" + textBox4.Text + "','" + comboBox2.Text + "','" + textBox6.Text + "','" + dateTimePicker1.Value.ToString() + "')"; 

如果还不行,那么可能是数据库结构和相应值的问题。

+0

请让我知道如果它没有工作 – Plycoder

+0

谢谢它的工作 –

+0

Welcome @ Precious Pari – Plycoder