C# 预订按钮
我正在制作一个班级预订网站,创建按钮时遇到问题。 我希望客户在两个文本框中插入两个详细信息会话,会话类型“class”或“workshop”以及日期和时间,他们将能够从显示的DataGridView中看到这些信息 一旦他们点击“Reserve”按钮,该按钮将运行一个查询,在该查询中将所选会话从session表添加到Reservation表。但是,我的代码执行时没有错误,但不会更新“Reservation”表 这是我的密码:C# 预订按钮,c#,sql,ms-access,C#,Sql,Ms Access,我正在制作一个班级预订网站,创建按钮时遇到问题。 我希望客户在两个文本框中插入两个详细信息会话,会话类型“class”或“workshop”以及日期和时间,他们将能够从显示的DataGridView中看到这些信息 一旦他们点击“Reserve”按钮,该按钮将运行一个查询,在该查询中将所选会话从session表添加到Reservation表。但是,我的代码执行时没有错误,但不会更新“Reservation”表 这是我的密码: OleDbConnection myConnection = GetCo
OleDbConnection myConnection = GetConnection();
OleDbCommand cmd = myConnection.CreateCommand();
string query = "select COUNT(*) from [Yoga-Session] where [session type] = '" + txt_type.Text + "' and duration = '" + txt_datetime.Text + "';";
OleDbCommand command = new OleDbCommand(query, myConnection);
myConnection.Open();
int rows1 = (Int32)command.ExecuteScalar();
if (rows1 >= 1)
{
cmd = new OleDbCommand("Select session_id from [yoga-session] where [session type] = '" + txt_type.Text + "' and duration = '" + txt_datetime.Text +"';",myConnection);
int classId = (Int32)command.ExecuteScalar();
cmd = new OleDbCommand("select client_id from client where name = '" + Session["[name]"] + "';", myConnection);
int clientID = (Int32)command.ExecuteScalar();
string query1 = "insert into reservation (session_id, client_id, client_name) values ('" + classId + "','" + clientID + "','" + Session["[name]"].ToString() + "');";
cmd = new OleDbCommand(query1, myConnection);
cmd.ExecuteNonQuery();
Response.Write("Reservation successful");
Response.Redirect("reservation.aspx");
myConnection.Close();
}
}
}
ExecuteNonQuery()
不适用于SELECT命令。要从SELECT命令获取字段值,我们需要使用ExecuteScalar()
尝试更换
int rows1 = (Int32)command.ExecuteScalar();
您需要使用cmd.ExecuteScalar()
来获取会话id
和客户端id
值ExecuteNonQuery
返回受SQL查询影响的行数
另外看看@Sherantha指出了什么。你应该使用query1而不是query
string query1 = "insert into reservation (session_id, client_id, client_name) values ('" + classId + "','" + clientID + "','" + Session["[name]"].ToString() + "');";
cmd = new OleDbCommand(query1, myConnection);// not query but query1
cmd.ExecuteNonQuery();
Response.Write("Reservation successful");
PS:使用sql数据读取器选择数据
cmd = new OleDbCommand("Select session_id from [yoga-session] where [session type] = '" + txt_type.Text + "' and duration = '" + txt_datetime.Text +"';",myConnection);
SqlDataReader rdr = cmd.ExecuteReader();
int classId = 0;
while (rdr.Read())
{
clientID = Convert.ToInt32(rdr["session_id"]);
}
在我写下我真正的答案之前,先做一个简短的开场白(因为我没有代表来评论) 第一:使用准备好的陈述。它们大大有助于减少键入SQL查询时的错误,同时也是在实际情况下防止SQL注入攻击的一种方法 第二:虽然在大多数数据库类型中并不真正需要,但建议在代码中严格统一命名约定 除此之外,我现在将得到真正的答案 查看代码,我假设
classID
和clientID
是整数,但在代码中,由于“”字符,它们似乎被解析为字符串。插入整数时不要使用字符
编辑:【会话类型】是指【会话类型】吗 嘿,我已经对classId和clientId进行了更改,但没有什么区别:(获取
session\u id
和client\u id
值,并在SSMS(Sql Server Mgmt Studio)中执行insert语句)。看看这是否会更新您想要的记录。如果不是,则表示您的数据不匹配,ADO.NET代码中没有任何错误。嘿,感谢您指出这个愚蠢的错误lol,但是我仍然没有得到任何结果:(我认为您尝试选择classID和客户端ID的方式是问题所在。请参阅我的编辑,并执行相同的操作以选择客户端ID和会话名称。一旦我应用了您的代码,我似乎无法在query1中使用classID和ClientID,我得到了“使用未分配的Local变量”在这两个变量@sheranthahey中,我都做了更改,但仍然没有得到任何结果@irshadrows1
的结果是什么?这一行的结果。调试它。int rows1=(Int32)command.ExecuteScalar();
hmm我将更改我的代码以使用准备好的语句,感谢大家的提醒..[Session Type]而不是[Session\u Type]@aeee98
cmd = new OleDbCommand("Select session_id from [yoga-session] where [session type] = '" + txt_type.Text + "' and duration = '" + txt_datetime.Text +"';",myConnection);
SqlDataReader rdr = cmd.ExecuteReader();
int classId = 0;
while (rdr.Read())
{
clientID = Convert.ToInt32(rdr["session_id"]);
}