我希望你能提供帮助,我正在尝试更新我的数据库文件,但我一直在收到错误,我不知道为什么,我没有任何问题添加数据或显示数据只是更新数据我遇到了什么问题?
错误是:
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 'Full_Name'.
这是我的源代码:
public void UpdateTable()
{
try
{
string ID = Txt_IdNumber.Text;//ID Input
string setYearFormat = Tdp_DateOfBirth.Value.ToString("yyyy");//Get only the year to calculate age
string CurrentYear = DateTime.Now.Year.ToString();//Get current year from system settings
int getAge = Convert.ToInt32(CurrentYear) - Convert.ToInt32(setYearFormat);//Calculate Age
string setDOB = Tdp_DateOfBirth.Value.ToString("dd/MM/yyyy");//Set TimeDatePicker to spesific Long date
string Query = "UPDATE UserData" +
"SET Full_Name = @Fullname, Date_Of_Birth = @DateOfBirth, ID_Number = @IdNumber, Age = @Age" +
"WHERE Id = @Id";//Update query
using (SqlConnection Connection = new SqlConnection(ConnectionString))//Connection to ConnectionString
using (SqlCommand Command = new SqlCommand(Query, Connection))//Sql Command to add/Update
{
Connection.Open();//Open Connection
Command.Parameters.AddWithValue("@Id", Dgv_Output.SelectedRows);//Add Values
Command.Parameters.AddWithValue("@Fullname", Txt_Fullname.Text);//Add Values
Command.Parameters.AddWithValue("@DateOfBirth", Convert.ToDateTime(setDOB));//Add Values
Command.Parameters.AddWithValue("@IdNumber", ID);//Add Values
Command.Parameters.AddWithValue("@Age", getAge);//Add Values
Command.ExecuteNonQuery();//Execute Non Query
}
Txt_Fullname.Clear();//Clear Textbox
Txt_IdNumber.Clear();//Clear Textbox
Tdp_DateOfBirth.Value = DateTime.Now;//Set TimeDatePicker to system date
Txt_Fullname.Focus();//Focus on Full Name textbox
}
catch (Exception Err)
{
MessageBox.Show(Err.ToString(), "ERROR");
using (StreamWriter sr = new StreamWriter("UpdateError.txt"))//Write error to file
{
sr.Flush();//Flush existing data
sr.WriteLine(Err);//Write new data
}
}
}
答案 0 :(得分:6)
您在sum = start;
do {
sum += step;
result.push(start);
}
和set
子句之前缺少空格:
where
答案 1 :(得分:0)
你必须用单引号括起@Full_Name:
' @ FULL_NAME'