我写的这个程序是为了插入,更新
使用来自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 = "";
}
}
}
}
毫无疑问,这个工作正常,请检查。
答案 0 :(得分: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()+"'";
答案 1 :(得分: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()+"'";
它应该:
=
符号,而不是*
。into
而不是in to
。你唯一有效的SQL就是SELECT。在将它们写入程序之前,您应该尝试在SQL Server Management Studio中运行查询。
答案 2 :(得分:1)
我猜你在使用“INSERT INTO info”时会使用“插入信息”。
答案 3 :(得分: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+"')"
答案 4 :(得分:0)
最佳实践
请勿使用Select *。在这里,您只需要查找2列数据。所以,只选择2个colunms。如果表有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());
}
}