-3

我为插入、更新编写的这个程序

使用来自 C# 数据库名称的数据集是信息,任何人都可以检查此代码,请帮助我应该在语法上写什么。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Insert_update_delete
{
    public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cmd.Connection = cn;
            Loadlist();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtId.Text != "" & txtName.Text != "")
            {
                cn.Open();
                cmd.CommandText = "Insert into info (Id, Name) Values('"+txtId.Text+"' , '"+txtName.Text+"')";
                cmd.ExecuteNonQuery();(it is showing error here )
                cmd.Clone();
                MessageBox.Show("Record Inserted!");
                cn.Close();
                txtId.Text = "";
                txtName.Text = "";
                Loadlist();
            }
        }
        private void Loadlist()
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            cn.Open();
            cmd.CommandText = "Select * From info";
            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    listBox1.Items.Add(dr[0].ToString());
                    listBox2.Items.Add(dr[1].ToString());
                }
            }

            cn.Close();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox l = sender as ListBox;
            if(l.SelectedIndex != -1)
            {
                listBox1.SelectedIndex = l.SelectedIndex;
                listBox2.SelectedIndex = l.SelectedIndex;
                txtId.Text = listBox1.SelectedItem.ToString();
                txtName.Text = listBox2.SelectedItem.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtId.Text != "" & txtName.Text != "")
            {
                cn.Open();
                cmd.CommandText = "Delete from info where id*'"+txtId.Text+"' and name*'"+txtName.Text+"'";
                cmd.ExecuteNonQuery();
                cn.Close();
                MessageBox.Show("Record Deleted");
                Loadlist();
                txtId.Text = "";
                txtName.Text = "";

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtId.Text != "" & txtName.Text != "" & listBox1.SelectedIndex != -1)
            {
                cn.Open();
                cmd.CommandText = "Update info set id='"+txtId.Text+"', name='"+txtName.Text+"' where id='"+listBox1.SelectedItem.ToString()+"' and name= '"+listBox2.SelectedItem.ToString()+"'";
                cmd.ExecuteNonQuery();
                cn.Close();
                MessageBox.Show("Record Updated");
                Loadlist();
                txtId.Text = "";
                txtName.Text = "";

            }
        }
    }
}

毫无疑问,这个工作正常,请检查。

4

5 回答 5

4

从插入语句中删除空格。你已经在它们之间放置了空格,in to所以into像这样改变你的命令文本。

   cmd.CommandText = "Insert into info (Id, Name) Values('"+txtId.Text+"' , '"+txtName.Text+"')";

还有一个是*你的删除语句中的内容。我认为当您忽略当前错误时,它也会给您带来问题

cmd.CommandText = "Delete from info where id*'"+txtId.Text+"' and name*'"+txtName.Text+"'";

所以应该

cmd.CommandText = "Delete from info where id=" + txtId.Text+"' and name='"+txtName.Text+"'";

现在你的更新命令:在这里你错过,了多列。你的更新命令应该是这样的

cmd.CommandText = "Update info set id='"+txtId.Text+"', name='"+txtName.Text+"' where id='"+listBox1.SelectedItem.ToString()+"' and name= '"+listBox2.SelectedItem.ToString()+"'";
于 2013-03-22T19:54:14.403 回答
3

此 SQL 不是有效的 SQL:

"Delete from info where id'"+txtId.Text+"' and name'"+txtName.Text+"'";
"Insert in to info (Id, Name) Values('"+txtId.Text+"' , '"+txtName.Text+"')";
"Update info set id='"+txtId.Text+"'name='"+txtName.Text+"' where id='"+listBox1.SelectedItem.ToString()+"' and name= '"+listBox2.SelectedItem.ToString()+"'";

它应该:

  1. =在 DELETE 中进行比较时包含符号,而不是*.
  2. 在 INSERT 中使用into而不是。in to
  3. UPDATE 中的字段之间有逗号。
  4. 不应连接 SQL,而应使用参数化查询。

您拥有的唯一有效 SQL 是 SELECT。在将查询写入程序之前,您应该尝试在 SQL Server Management Studio 中运行查询。

于 2013-03-22T19:54:35.507 回答
1

我猜你应该使用“INSERT INTO info”时使用的是“Insert in to info”。

于 2013-03-22T19:53:42.507 回答
0

看起来您的插入命令中有错字:

cmd.CommandText = "Insert in to info (Id, Name) Values('"+txtId.Text+"' , '"+txtName.Text+"')"

应该是这样的:

cmd.CommandText = "Insert into info (Id, Name) Values('"+txtId.Text+"' , '"+txtName.Text+"')"
于 2013-03-22T19:55:00.477 回答
0

最佳实践

不要使用选择 *。在这里,您只查找 2 列数据。因此,仅选择 2 个列。如果表有 20 列怎么办?那是 18 个额外的数据列,被无偿检索。此外,请参考列名而不是索引。如果将来有人更改该表的架构并在 0 和 1 列之间插入列,那么您的结果可能不是预期的。

Select col1, col2 from info

你的代码:

cmd.CommandText = "Select * From info";
            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    listBox1.Items.Add(dr[0].ToString());
                    listBox2.Items.Add(dr[1].ToString());
                }
            }
于 2013-03-22T20:00:01.723 回答