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個值,但我得到錯誤值不匹配。
請幫我解決這個問題。我重視
錯誤我得到的是這裏顯示的代碼和問題截圖:
數據庫模式截圖在這裏
請讓我知道如果你的問題還沒有解決 – Plycoder
可能重複的[sql server:列名或提供的值數量不匹配表定義](http://stackoverflow.com/questions/20372361/sql- server-column-name-or-number-of-supplied-values-does-not-match-table-defini) – Shell
[SQL注入警報](http://msdn.microsoft.com/zh-cn/library/ms161953 %28v = sql.105%29.aspx) - 你應該不**連接你的SQL語句 - 使用**參數化查詢**來代替以避免SQL注入 –