[英]Sql C# Incorrect syntax near
我試圖編寫代碼以將所有數據從網格插入表中。 在網格中,我顯示了我所需要的,這不是問題,或者沒有給出錯誤
顯示此錯誤:
System.Data.SqlClient.SqlException:“ {”附近的語法不正確
string StrQuery;
try
{
using (SqlConnection conn = new SqlConnection(stringcon))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
conn.Open();
for (int i = 1; i < bunifuCustomDataGrid2.Rows.Count; i++)
{
StrQuery = @"INSERT INTO concediati VALUES ("
+ bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString() + ", "
+ bunifuCustomDataGrid2.Rows[i].Cells["lastname"].ToString() + ", "
+ bunifuCustomDataGrid2.Rows[i].Cells["CARS"].ToString() + ", "
+ bunifuCustomDataGrid2.Rows[i].Cells["RENT"].ToString() + ", "
+ bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].ToString() + ");";
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
}
}
}
catch (Exception)
{
throw;
}
更新了參數。
string StrQuery;
try
{
using (SqlConnection conn = new SqlConnection(stringcon))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
conn.Open();
for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)
{
StrQuery = @"INSERT INTO concediati(nume,prenume,idcar,idrent,idclient) VALUES (@name,@lastname,@car,@rent,@client)";
comm.Parameters.AddWithValue("@name", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString()));
comm.Parameters.AddWithValue("@lastname", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].ToString()));
comm.Parameters.AddWithValue("@car", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].ToString()));
comm.Parameters.AddWithValue("@rent", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].ToString()));
comm.Parameters.AddWithValue("@client", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].ToString()));
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
}
}
}
catch (Exception)
{
throw;
}
現在,它給出了另一個錯誤:
System.FormatException:'輸入字符串的格式不正確。
圖片: capture1
capture25
capture25
capture25
capture5
表:
CREATE TABLE [dbo].[concediati] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[nume] VARCHAR (50) NULL,
[prenume] VARCHAR (50) NULL,
[idclient] INT NULL,
[idrent] INT NULL,
[idcar] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString())
為您提供ToString
方法的替代實現。 這意味着您沒有從上述代碼中獲得實際值。 您應該改用bunifuCustomDataGrid2.Rows[i].Cells["firstname"].Value
。
如果有幫助,請標記為已回答。
INSERT的文檔在表名和列列表之間顯示一個空格,因此最好遵循該空格。
另外,您可以在循環外創建一次參數,然后在循環中設置它們的值(否則,您需要在參數上調用.Clear(),並在每次迭代時重新創建它們):
string sql = @"INSERT INTO concediati (nume, prenume, idcar, idrent, idclient) VALUES (@name, @lastname, @car, @rent, @client)";
using (SqlConnection conn = new SqlConnection(stringcon))
{
using (SqlCommand comm = new SqlCommand(sql, conn))
{
comm.Parameters.Add(new SqlParameter { ParameterName = "@name", SqlDbType = SqlDbType.VarChar, Size = 50 });
comm.Parameters.Add(new SqlParameter { ParameterName = "@lastname", SqlDbType = SqlDbType.VarChar, Size = 50 });
comm.Parameters.Add(new SqlParameter { ParameterName = "@car", SqlDbType = SqlDbType.Int });
comm.Parameters.Add(new SqlParameter { ParameterName = "@rent", SqlDbType = SqlDbType.Int });
comm.Parameters.Add(new SqlParameter { ParameterName = "@client", SqlDbType = SqlDbType.Int });
conn.Open();
for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)
{
string firstName = Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].Value);
string lastName = Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].Value);
int car = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].Value);
int rent = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].Value);
int client = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].Value);
comm.Parameters["@name"].Value = firstName;
comm.Parameters["@lastname"].Value = lastName;
comm.Parameters["@car"].Value = car;
comm.Parameters["@rent"].Value = rent;
comm.Parameters["@client"].Value = client;
comm.ExecuteNonQuery();
}
}
}
我檢查了您的代碼並進行了更改。 您可以使用以下代碼。
string StrQuery;
try
{
using (SqlConnection conn = new SqlConnection(stringcon))
{
for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)
{
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
StrQuery = @"INSERT INTO concediati(nume,prenume,idcar,idrent,idclient) VALUES (@name,@lastname,@car,@rent,@client)";
comm.Parameters.AddWithValue("@name", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString()));
comm.Parameters.AddWithValue("@lastname", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].ToString()));
comm.Parameters.AddWithValue("@car", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].ToString()));
comm.Parameters.AddWithValue("@rent", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].ToString()));
comm.Parameters.AddWithValue("@client", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].ToString()));
comm.CommandText = StrQuery;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
}
}
catch (Exception ex)
{
throw;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.