我收到此错误:
System.Data.SqlClient.SqlException:'关键字'Table'附近的语法不正确。
当我运行程序时;它说错误靠近桌子!
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 WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string SOURCE = @"Data Source=DESKTOP-K39PU4T\SQLEXPRESS;Initial Catalog=Mohamed;Integrated Security=True";
SqlConnection CON = new SqlConnection(SOURCE);
CON.Open();
MessageBox.Show("DB Connected");
string SqlSelectQuery = " Select*From Table Where ID ="+ int.Parse(textBox1.Text);
SqlCommand cmd = new SqlCommand(SqlSelectQuery, CON);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
textBox2.Text = (dr["Name"].ToString());
textBox3.Text = (dr["Surname"].ToString());
textBox4.Text = (dr["Lastname"].ToString());
}
else
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
MessageBox.Show("No Record Found Please Enter Correct Id");
}
CON.Close();
}
}
}
我想在SQL Studio中将数据从SQL Server加载到ASP.NET
答案 0 :(得分:1)
表是关键词,如果你有一个名为"表"您可能需要在SQL字符串中使用[Table]
表示escape关键字,否则请使用正确的表名而不是Table。你也最好使用参数而不是将字符串连接为sql语句。
string SqlSelectQuery = "Select * From [Table] Where ID =@ID";
SqlCommand cmd = new SqlCommand(SqlSelectQuery, CON);
cmd.Parameters.AddWithValue("@ID", int.Parse(textBox1.Text));
答案 1 :(得分:0)
您想从中获取数据的表名是什么?
如果其名称为Table,则将“Select * From Table Where ID =”替换为“Select * From \”Table \“Where ID =” 否则将表替换为实际的表名