SQL 语句末尾缺少分号 (;)。使用 C# 访问数据库
Missing semicolon (;) at end of SQL statement.. Access Database With C#
在下面的代码中,我想在 C# 中的单个命令中添加多行
但是当我执行代码时,它会抛出异常并出现此错误:SQL 语句末尾缺少分号 (;)。
我搜索了这个问题,但这种问题通常发生在
插入和位置
This post
和
我不知道为什么这不起作用。
public void Main_InsertAllDatas(int currentUser, List<PasswordData> input)
{
// Query Execution
// Create new command
System.Data.OleDb.OleDbCommand Command = new System.Data.OleDb.OleDbCommand();
// Set connection
Command.Connection = MainConnection;
// Query
string strQuery = "INSERT INTO [tbl_Data] ([UserName],[Password],[Category],[Title],[Description],[UserID]) VALUES ";
for (int i = 0; i < input.Count; i++)
{
strQuery += "(";
strQuery += "@iUserName" + i.ToString();
strQuery += ",";
strQuery += "@iPassword" + i.ToString();
strQuery += ",";
strQuery += "@iCategory" + i.ToString();
strQuery += ",";
strQuery += "@iTitle" + i.ToString();
strQuery += ",";
strQuery += "@iDescription" + i.ToString();
strQuery += ",";
strQuery += "@iUserID" + i.ToString();
strQuery += ")";
Command.Parameters.AddWithValue("@iUserName" + i.ToString(), EncDecWorker.EncrypteString(input[i].Username, Key));
Command.Parameters.AddWithValue("@iPassword" + i.ToString(), EncDecWorker.EncrypteString(input[i].Password, Key));
Command.Parameters.AddWithValue("@iCategory" + i.ToString(), EncDecWorker.EncrypteString(input[i].Category, Key));
Command.Parameters.AddWithValue("@iTitle" + i.ToString(), EncDecWorker.EncrypteString(input[i].Title, Key));
Command.Parameters.AddWithValue("@iDescription" + i.ToString(), EncDecWorker.EncrypteString(input[i].Description, Key));
Command.Parameters.AddWithValue("@iUserID" + i.ToString(), input[i].UserID);
if (i == input.Count - 1)
{
break;
}
strQuery += ",";
}
strQuery += ";";
Command.CommandText = strQuery;
try
{
MainConnection.Open();
Command.ExecuteNonQuery();
MainConnection.Close();
}
catch (Exception)
{
if (MainConnection.State == ConnectionState.Open)
{
MainConnection.Close();
}
}
finally
{
MainConnection.Close();
}
}
谢谢你的帮助。
您正在循环中添加括号。 MS SQL-Server < 2008, MS Access doesn't support it 甚至不支持该插入语法。您还一次又一次地添加相同的参数,而不是一次创建参数并覆盖循环中的 Value
。还对实现 IDisposable
:
的所有内容使用 using
语句
public void Main_InsertAllDatas(int currentUser, List<PasswordData> input)
{
string insert = "INSERT INTO tbl_Data ([UserName],[Password],[Category],[Title],[Description],[UserID]) VALUES (@UserName,@Password,@Category,@Title,@Description,@UserID)";
using (var MainConnection = new System.Data.OleDb.OleDbConnection("..."))
using (var Command = new System.Data.OleDb.OleDbCommand(insert, MainConnection))
{
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@UserName", System.Data.OleDb.OleDbType.VarChar, 50));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Password", System.Data.OleDb.OleDbType.VarChar, 50));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Category", System.Data.OleDb.OleDbType.VarChar, 100));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Title", System.Data.OleDb.OleDbType.VarChar, 100));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Description", System.Data.OleDb.OleDbType.VarChar, 100));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@UserID", System.Data.OleDb.OleDbType.Integer));
MainConnection.Open();
for (int i = 0; i < input.Count; i++)
{
Command.Parameters["@UserName"].Value = EncDecWorker.EncrypteString(input[i].Username, Key);
// ...
try
{
MainConnection.Open();
int inserted = Command.ExecuteNonQuery();
} catch (Exception)
{
// log, show or do something else useful
throw; // better than an empty catch
}
}
} // you don't need to close the connection with using
}
在下面的代码中,我想在 C# 中的单个命令中添加多行
但是当我执行代码时,它会抛出异常并出现此错误:SQL 语句末尾缺少分号 (;)。
我搜索了这个问题,但这种问题通常发生在
插入和位置
This post
和
我不知道为什么这不起作用。
public void Main_InsertAllDatas(int currentUser, List<PasswordData> input)
{
// Query Execution
// Create new command
System.Data.OleDb.OleDbCommand Command = new System.Data.OleDb.OleDbCommand();
// Set connection
Command.Connection = MainConnection;
// Query
string strQuery = "INSERT INTO [tbl_Data] ([UserName],[Password],[Category],[Title],[Description],[UserID]) VALUES ";
for (int i = 0; i < input.Count; i++)
{
strQuery += "(";
strQuery += "@iUserName" + i.ToString();
strQuery += ",";
strQuery += "@iPassword" + i.ToString();
strQuery += ",";
strQuery += "@iCategory" + i.ToString();
strQuery += ",";
strQuery += "@iTitle" + i.ToString();
strQuery += ",";
strQuery += "@iDescription" + i.ToString();
strQuery += ",";
strQuery += "@iUserID" + i.ToString();
strQuery += ")";
Command.Parameters.AddWithValue("@iUserName" + i.ToString(), EncDecWorker.EncrypteString(input[i].Username, Key));
Command.Parameters.AddWithValue("@iPassword" + i.ToString(), EncDecWorker.EncrypteString(input[i].Password, Key));
Command.Parameters.AddWithValue("@iCategory" + i.ToString(), EncDecWorker.EncrypteString(input[i].Category, Key));
Command.Parameters.AddWithValue("@iTitle" + i.ToString(), EncDecWorker.EncrypteString(input[i].Title, Key));
Command.Parameters.AddWithValue("@iDescription" + i.ToString(), EncDecWorker.EncrypteString(input[i].Description, Key));
Command.Parameters.AddWithValue("@iUserID" + i.ToString(), input[i].UserID);
if (i == input.Count - 1)
{
break;
}
strQuery += ",";
}
strQuery += ";";
Command.CommandText = strQuery;
try
{
MainConnection.Open();
Command.ExecuteNonQuery();
MainConnection.Close();
}
catch (Exception)
{
if (MainConnection.State == ConnectionState.Open)
{
MainConnection.Close();
}
}
finally
{
MainConnection.Close();
}
}
谢谢你的帮助。
您正在循环中添加括号。 MS SQL-Server < 2008, MS Access doesn't support it 甚至不支持该插入语法。您还一次又一次地添加相同的参数,而不是一次创建参数并覆盖循环中的 Value
。还对实现 IDisposable
:
using
语句
public void Main_InsertAllDatas(int currentUser, List<PasswordData> input)
{
string insert = "INSERT INTO tbl_Data ([UserName],[Password],[Category],[Title],[Description],[UserID]) VALUES (@UserName,@Password,@Category,@Title,@Description,@UserID)";
using (var MainConnection = new System.Data.OleDb.OleDbConnection("..."))
using (var Command = new System.Data.OleDb.OleDbCommand(insert, MainConnection))
{
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@UserName", System.Data.OleDb.OleDbType.VarChar, 50));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Password", System.Data.OleDb.OleDbType.VarChar, 50));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Category", System.Data.OleDb.OleDbType.VarChar, 100));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Title", System.Data.OleDb.OleDbType.VarChar, 100));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Description", System.Data.OleDb.OleDbType.VarChar, 100));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@UserID", System.Data.OleDb.OleDbType.Integer));
MainConnection.Open();
for (int i = 0; i < input.Count; i++)
{
Command.Parameters["@UserName"].Value = EncDecWorker.EncrypteString(input[i].Username, Key);
// ...
try
{
MainConnection.Open();
int inserted = Command.ExecuteNonQuery();
} catch (Exception)
{
// log, show or do something else useful
throw; // better than an empty catch
}
}
} // you don't need to close the connection with using
}