繁体   English   中英

C#.net Winforms数据插入错误

[英]C# .net winforms data insert error

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窗体应用程序,我试图将数据插入数据库中,但是即使输入了正确的属性值,也出现了值错误和列不匹配的错误。

我有8个属性(具有自动递增的ID),对于其余7列,我正在输入7个值,但出现值不匹配的错误。

请帮我解决这个问题。 我已附上问题的代码和屏幕截图

我得到的错误显示在这里:

在此处输入图片说明

数据库架构屏幕截图在这里

在此处输入图片说明

('" + 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 )。

仔细看你的插入查询是有,(昏迷)失踪。 您能否按照以下说明更正您的查询:

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

如果仍然无法使用,则数据库结构和相应的值可能有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM