System.Data.dll中发生未处理的“System.Data.SqlClient.SqlException”类型的异常附加信息:无效的列名称“5th”。 [英] An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Invalid column name '5th'.
问题描述
我正在制作电话簿,但这行中有一个错误
cmd.ExecuteNonQuery();在删除功能请告诉我什么
我做错了我的问题是,如果我单击删除按钮删除表中的数据,则会出现此错误,数据不会从数据库中删除我的c#代码是这样的: -
使用System;
使用System.Collections.Generic;
使用System .ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Data.SqlClient;
名称空间电话
{
公共部分课程电话:表格
{
SqlConnection con = new SqlConnection(@Data Source = ZOHAIB\SQLEXPRESS; Initial Catalog = bscs; Integrated Security = True);
公共电话()
{
InitializeC omponent();
}
private void Phone_Load(object sender,EventArgs e)
{
显示();
}
private void button1_Click(对象发送者,EventArgs e)
{
textBox1.Text =;
textBox2.Clear();
textBox3.Text =;
textBox4。清除();
comboBox1.SelectedIndex = -1;
textBox1.Focus();
}
private void button2_Click(object sender,EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand(@INSERT INTO [5th]
([名字],[姓氏],[手机] ,[电子邮件],[Catagory])
VALUES('+ textBox1.Text + ,'+ textBox2.Text +','+ textBox3.Text +','+ textBox4.Text +','+ comboBox1.Text +'),con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(sucessfuly saved);
显示();
}
void显示()
{
SqlDataAdapter sda = new SqlDataAdapter(select *来自[5th],con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1 .Rows.Clear();
foreach(dt.Rows中的DataRow项目)
{
int n = dataGridView1.Rows.Add( );
dataGridView1.Rows [n] .Cells [0] .Value = item [First Name]。ToString();
dataGridView1.Rows [n] .Cells [1] .Value = item [1] .ToString();
dataGridView1.Rows [n] .Cells [2] .Valu e = item [2] .ToString();
dataGridView1.Rows [n] .Cells [3] .Value = item [3] .ToString();
dataGridView1.Rows [n] .Cells [4] .Value = item [4] .ToString();
}
}
private void dataGridView1_MouseClick(object sender,MouseEventArgs e)
{
textBox1.Text = dataGridView1.SelectedRows [0] .Cells [0] .Value.ToString();
textBox2.Text = dataGridView1.SelectedRows [0] .Cells [1] .Value.ToString();
textBox3.Text = dataGridView1.SelectedRows [0] .Cells [2] .Value.ToString();
textBox4.Text = dataGridView1.SelectedRows [0] .Cells [3] .Value.ToString();
comboBox1.Text = dataGridView1.SelectedRows [0] .Cells [4 ] .Value.ToString();
}
private void button3_Click(object sender,Event Args e)
{
con.Open();
SqlCommand cmd = new SqlCommand(@删除[5]
WHERE([5th] ='+ textBox1.Text +'),con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(删除成功);
显示();
}
}
}>
I am making telephone directory but one error is coming in this line "
cmd.ExecuteNonQuery();" in the delete functionality please tell me what
I am doing wrong and My problem is that if I click the delete button to delete the data from table this error appears and data does not delete from database my c# code is something like this :-
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 Telephone
{
public partial class Phone : Form
{
SqlConnection con = new SqlConnection(@"Data Source = ZOHAIB\SQLEXPRESS; Initial Catalog = bscs; Integrated Security = True");
public Phone()
{
InitializeComponent();
}
private void Phone_Load(object sender, EventArgs e)
{
Display();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Clear();
textBox3.Text = "";
textBox4.Clear();
comboBox1.SelectedIndex = -1;
textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO [5th]
([First Name], [Last Name], [Mobile], [Email], [Catagory])
VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + comboBox1.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("sucessfuly saved");
Display();
}
void Display()
{
SqlDataAdapter sda = new SqlDataAdapter("select * from [5th]", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["First Name"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item[1].ToString();
dataGridView1.Rows[n].Cells[2].Value = item[2].ToString();
dataGridView1.Rows[n].Cells[3].Value = item[3].ToString();
dataGridView1.Rows[n].Cells[4].Value = item[4].ToString();
}
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text= dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox2.Text= dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
textBox3.Text=dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
comboBox1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand(@"DELETE FROM [5th]
WHERE ([5th]='" + textBox1.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Delete sucessfully");
Display();
}
}
}>
推荐答案
只是猜测,但是:
Just guessing, but:
SqlCommand cmd = new SqlCommand(@"DELETE FROM [5th] WHERE ([5th]='" + textBox1.Text + "')", con);
你真的在你的表calle中有一个名为 5th
的列d 第五
?
我对此表示怀疑。
但更重要的是,不要做那样的事情!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
Do you really have a column called 5th
in your table called 5th
?
I doubt it.
But more importantly, don't do any of that like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
这篇关于System.Data.dll中发生未处理的“System.Data.SqlClient.SqlException”类型的异常附加信息:无效的列名称“5th”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!