文本未保存在我的数据库中? [英] Text not saving in my database?

查看:63
本文介绍了文本未保存在我的数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次按下我的按钮时都会显示



System.Data.dll中出现未处理的System.Data.SqlClient.SqlException类型异常

附加信息:关键字'DataBase'附近的语法不正确。



关键字'SET'附近的语法不正确。



你能帮帮我吗?



我尝试过:



 private void button1_Click(object sender,EventArgs e)
{
con = new SqlConnection(@Data Source =(LocalDB)\ MSSQLLocalDB ; AttachDbFilename = C:\ Users \ _djino_5yu1pcr \Documents\DataSet.mdf; I ntegrated Security = True; Connect Timeout = 30);
con.Open();
cmd = new SqlCommand(Update DataBase Set(Serial,Name)VALUES(@ Serial,@ Name),con);
cmd.Parameters.Add(@ Serial,txtSerial.Text);
cmd.Parameters.Add(@ Name,txtName.Text);
cmd.ExecuteNonQuery();
}

解决方案

你做了一个UPSERT语句(这不是一个存在的东西)。您的UPDATE语句是错误的......看起来您混合了INSERT和UPDATE的格式。它应格式化如下



 更新 数据库  SET  Serial =  @ Serial ,Name =  @ Name ; 





因此,这将是

 UPDATE< TableName> ; SET< ColumnName> =< ColumnValue>,< ColumnName> =< ColumnValue> 





或单列更新



 UPDATE< TableName> SET< ColumnName> =< ColumnValue> 


假设您正在尝试更新名为DataBase的表,则存在两个问题:



1)您的UPDATE命令语法错误(请参阅注释中NPC提供的链接)。



2)数据库是保留字SQL。使用保留字作为对象名称是可以的,但是当你引用它们时你需要用方括号来转义它们。



因此,你的SQL应该看起来像这个:



 更新 [ DataBase ]  SET  Serial =  @ Serial ,Name =  @ Name  





请注意,这将更新表中的每一行,因为没有WHERE子句。


Each time when I press my button it says

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near the keyword 'DataBase'.

Incorrect syntax near the keyword 'SET'.

Could you help me?

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
  con = new SqlConnection(@"Data Source=        (LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\djino_5yu1pcr\Documents\DataSet.mdf;I  ntegrated Security=True;Connect Timeout=30");
  con.Open();
  cmd = new SqlCommand("Update DataBase Set (Serial,Name) VALUES (@Serial,@Name)", con);
  cmd.Parameters.Add("@Serial", txtSerial.Text);
  cmd.Parameters.Add("@Name", txtName.Text);
  cmd.ExecuteNonQuery();
}

解决方案

You've made an UPSERT statement (which isn't a thing that exists). Your UPDATE statement is wrong...it looks like you mixed the formatting of INSERT and UPDATE. It should be formatted as follows

UPDATE Database SET Serial = @Serial, Name = @Name;



So that would be

UPDATE <TableName> SET <ColumnName> = <ColumnValue>,<ColumnName> = <ColumnValue>



or for single column updates

UPDATE <TableName> SET <ColumnName> = <ColumnValue>


Assuming that you are trying to update a table called DataBase, there are two problems:

1) Your UPDATE command syntax is wrong (See the link provided by NPC in the comments).

2) Database is a reserved word in SQL. It's okay to use reserved words as object names but you'll need to escape them with square brackets when you refer to them.

As such, your SQL should look like this:

UPDATE [DataBase] SET Serial = @Serial, Name = @Name



Note that that will update every row in the table as there is no WHERE clause.


这篇关于文本未保存在我的数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆