使用 C# 更新 SQL table
Update SQL table using C#
我正在尝试更新剧集 table 中的 EpisodeId no:117 并且它执行成功,但是当我检查 table 时它没有更新。 int 剧集 ID = 117;
int seriesNumber = 9;
int episodeNumber = 13;
string episodeType = "abnormal episode";
string title = "Reconsideration";
string notes = "recuring behaviour";
//connectionString
string connectionString = "data source=LAPTOP-VLO4EFFQ\MSSQLSERVER01; database=DoctorWho; integrated Security=True;";
//connection using
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
Console.WriteLine("Connection sucessfull");
string query = "UPDATE tblEpisode " +
"(SeriesNumber, EpisodeNumber, EpisodeType, Title, Notes)" +
"(SET SeriesNumber=@SeriesNumber, EpisodeNumber=@EpisodeNumber, EpisodeType=@EpisodeType, Title=@Title, Notes=@Notes)" +
"(WHERE EpisodeId=@EpisodeId)";
using (SqlCommand command = new SqlCommand(query, conn))
{
//updating data in the sql table with the initial variables
command.Parameters.Add("@EpisodeId", System.Data.SqlDbType.Int).Value = episodeId;
command.Parameters.Add("@SeriesNumber", System.Data.SqlDbType.Int).Value = seriesNumber;
command.Parameters.Add("@EpisodeNumber", System.Data.SqlDbType.Int).Value = episodeNumber;
command.Parameters.Add("@EpisodeType", System.Data.SqlDbType.NVarChar).Value = episodeType;
command.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar).Value = title;
command.Parameters.Add("@Notes", System.Data.SqlDbType.NVarChar).Value = notes;
}
conn.Close();
Console.WriteLine("connection is closed!!");
}
您的 SQL 更新 statement.Look 存在一些问题,请参考 SQL LINK
中的更新声明
还有一种更简单的方法可以使用 AddWithValue 方法添加参数。 LINK
接下来,你不是在执行SQL命令。对于 Update 语句,请使用 ExecuteNonQuery() 方法。 LINK
同样如@Nikki9696 所述,未声明 episodeId。确保使用其他变量声明 episodeId。
int episodeId = 117;
int seriesNumber = 9;
int episodeNumber = 13;
string episodeType = "abnormal episode";
string title = "Reconsideration";
string notes = "recuring behaviour";
//connectionString
string connectionString = "data source=LAPTOP-VLO4EFFQ\MSSQLSERVER01; database=DoctorWho; integrated Security=True;";
//connection using
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
Console.WriteLine("Connection sucessfull");
string query = "UPDATE tblEpisode " +
"SET SeriesNumber=@SeriesNumber, EpisodeNumber=@EpisodeNumber, EpisodeType=@EpisodeType, Title=@Title, Notes=@Notes " +
" WHERE EpisodeId=@EpisodeId;";
using (SqlCommand command = new SqlCommand(query, conn))
{
//updating data in the sql table with the initial variables
command.Parameters.AddWithValue("@EpisodeId", episodeId);
command.Parameters.AddWithValue("@SeriesNumber", seriesNumber);
command.Parameters.AddWithValue("@EpisodeNumber", episodeNumber);
command.Parameters.AddWithValue("@EpisodeType", episodeType);
command.Parameters.AddWithValue("@Title", title);
command.Parameters.AddWithValue("@Notes", notes);
command.ExecuteNonQuery();
}
conn.Close();
Console.WriteLine("connection is closed!!");
}
我正在尝试更新剧集 table 中的 EpisodeId no:117 并且它执行成功,但是当我检查 table 时它没有更新。 int 剧集 ID = 117;
int seriesNumber = 9;
int episodeNumber = 13;
string episodeType = "abnormal episode";
string title = "Reconsideration";
string notes = "recuring behaviour";
//connectionString
string connectionString = "data source=LAPTOP-VLO4EFFQ\MSSQLSERVER01; database=DoctorWho; integrated Security=True;";
//connection using
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
Console.WriteLine("Connection sucessfull");
string query = "UPDATE tblEpisode " +
"(SeriesNumber, EpisodeNumber, EpisodeType, Title, Notes)" +
"(SET SeriesNumber=@SeriesNumber, EpisodeNumber=@EpisodeNumber, EpisodeType=@EpisodeType, Title=@Title, Notes=@Notes)" +
"(WHERE EpisodeId=@EpisodeId)";
using (SqlCommand command = new SqlCommand(query, conn))
{
//updating data in the sql table with the initial variables
command.Parameters.Add("@EpisodeId", System.Data.SqlDbType.Int).Value = episodeId;
command.Parameters.Add("@SeriesNumber", System.Data.SqlDbType.Int).Value = seriesNumber;
command.Parameters.Add("@EpisodeNumber", System.Data.SqlDbType.Int).Value = episodeNumber;
command.Parameters.Add("@EpisodeType", System.Data.SqlDbType.NVarChar).Value = episodeType;
command.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar).Value = title;
command.Parameters.Add("@Notes", System.Data.SqlDbType.NVarChar).Value = notes;
}
conn.Close();
Console.WriteLine("connection is closed!!");
}
您的 SQL 更新 statement.Look 存在一些问题,请参考 SQL LINK
中的更新声明还有一种更简单的方法可以使用 AddWithValue 方法添加参数。 LINK
接下来,你不是在执行SQL命令。对于 Update 语句,请使用 ExecuteNonQuery() 方法。 LINK
同样如@Nikki9696 所述,未声明 episodeId。确保使用其他变量声明 episodeId。
int episodeId = 117;
int seriesNumber = 9;
int episodeNumber = 13;
string episodeType = "abnormal episode";
string title = "Reconsideration";
string notes = "recuring behaviour";
//connectionString
string connectionString = "data source=LAPTOP-VLO4EFFQ\MSSQLSERVER01; database=DoctorWho; integrated Security=True;";
//connection using
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
Console.WriteLine("Connection sucessfull");
string query = "UPDATE tblEpisode " +
"SET SeriesNumber=@SeriesNumber, EpisodeNumber=@EpisodeNumber, EpisodeType=@EpisodeType, Title=@Title, Notes=@Notes " +
" WHERE EpisodeId=@EpisodeId;";
using (SqlCommand command = new SqlCommand(query, conn))
{
//updating data in the sql table with the initial variables
command.Parameters.AddWithValue("@EpisodeId", episodeId);
command.Parameters.AddWithValue("@SeriesNumber", seriesNumber);
command.Parameters.AddWithValue("@EpisodeNumber", episodeNumber);
command.Parameters.AddWithValue("@EpisodeType", episodeType);
command.Parameters.AddWithValue("@Title", title);
command.Parameters.AddWithValue("@Notes", notes);
command.ExecuteNonQuery();
}
conn.Close();
Console.WriteLine("connection is closed!!");
}