C# 在发布一个(BookAvailability-1)之前,请检查SQL的Book\u可用性
如果我将“If、foreach和else语句放在comment/”下,程序就会工作,并从SQL数据库中将书籍计数减少1。但我想看看是否至少有一本书可以赠送。如果我这样离开,这段代码会一直在“else”语句中显示消息。需要快速帮助,这是我的最后一个项目,需要在2007年7月23日之前完成(C# 在发布一个(BookAvailability-1)之前,请检查SQL的Book\u可用性,c#,sql-server,database-connection,C#,Sql Server,Database Connection,如果我将“If、foreach和else语句放在comment/”下,程序就会工作,并从SQL数据库中将书籍计数减少1。但我想看看是否至少有一本书可以赠送。如果我这样离开,这段代码会一直在“else”语句中显示消息。需要快速帮助,这是我的最后一个项目,需要在2007年7月23日之前完成( 您只检查结果集中最后一行的图书数量,而不是所有行的图书可用性。您可能需要执行以下操作: SqlCommand cmd2 = connection.CreateCommand();
您只检查结果集中最后一行的图书数量,而不是所有行的图书可用性。您可能需要执行以下操作:
SqlCommand cmd2 = connection.CreateCommand();
cmd2.CommandType = CommandType.Text;
cmd2.CommandText = "SELECT BookAvailability FROM Book_list WHERE BookName = '" + TextBoxBookName + "'";
var result = cmd2.ExecuteScalar();
book_qty = Convert.ToInt32(result);
您需要确保只有一本具有给定书名的书可用
在这种情况下,只需更正代码中的这一行也会有所帮助:
book_qty = Convert.ToInt32(dr2["book_qty"].ToString());
到
否则,您需要查询SUM(BookAvailability),但下面的代码会一次减少多本书的图书数量,这不太好。未测试的代码。我没有您的数据库。注释和解释在同一行
private void OPCode()
{
try
{
//keep your connections close to the vest (local)
using (SqlConnection connection = new SqlConnection())
//a using block ensures that your objects are closed and disposed
//even if there is an error
{
using (SqlCommand cmd2 = new SqlCommand("SELECT BookAvailability FROM Book_list WHERE BookName = @BookName", connection))
{
//Always use parameters to protect from sql injection
//Also it is easier than fooling with the single quotes etc.
//If you are referring to a TextBox you need to provide what property is
//being accessed. I am not in a WPF right now and not sure if .Text
//is correct; may be .Content
//You need to check your database for correct data type and field size
cmd2.Parameters.Add("@BookName", SqlDbType.VarChar, 100).Value = TextBoxBookName.Text;
//A select statement is not a non-query
//You don't appear to be using the data table or data adapter
//so dump them extra objects just slow things dowm
connection.Open();
//Comment out the next 2 lines and replaced with
//Edit Update
//var returnVal = cmd2.ExecuteScalar() ?? 0;
//if ((int)returnVal > 0)
//*************************************************************
//Edit Update
//*************************************************************
//in case the query returns a null, normally an integer cannot
//hold the value of null so we use nullable types
// the (int?) casts the result of the query to Nullable of int
Nullable<int> returnVal = (int?)cmd2.ExecuteScalar();
//now we can use the .GetValueOrDefault to return the value
//if it is not null of the default value of the int (Which is 0)
int bookCount = returnVal.GetValueOrDefault();
//at this point bookCount should be a real int - no cast necessary
if (bookCount > 0)
//**************************************************************
//End Edit Update
//**************************************************************
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO issue_book VALUES(@SearchMembers etc", connection))
{
//set up the parameters for this command just like the sample above
cmd.Parameters.Add("@SearchMembers", SqlDbType.VarChar, 100).Value = TextBoxSearchMembers.Text;
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd1 = new SqlCommand("UPDATE Book_list SET BookAvailability = BookAvailability-1 WHERE BookName = @BoxBookName;", connection))
{
cmd1.Parameters.Add("@BoxBookName", SqlDbType.VarChar, 100);
cmd1.ExecuteNonQuery();
}
MessageBox.Show("success");
this.Close();
}
else
{
MessageBox.Show("Book not available");
}
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
private void操作码()
{
尝试
{
//将您的连接保持在背心附近(本地)
使用(SqlConnection=newsqlconnection())
//使用块可确保对象被关闭和释放
//即使有错误
{
使用(SqlCommand cmd2=newsqlcommand(“从Book_列表中选择BookAvailability,其中BookName=@BookName”,连接))
{
//始终使用参数来防止sql注入
//而且,这比愚弄单引号等更容易。
//如果您引用的是文本框,则需要提供属性是什么
//正在被访问。我现在不在WPF中,也不确定是否.Text
//是正确的;可能是。内容
//您需要检查数据库中的数据类型和字段大小是否正确
cmd2.Parameters.Add(“@BookName”,SqlDbType.VarChar,100)。Value=TextBoxBookName.Text;
//select语句不是非查询
//您似乎没有使用数据表或数据适配器
//所以把多余的东西扔给他们,让事情变慢
connection.Open();
//注释掉接下来的两行并替换为
//编辑更新
//var returnVal=cmd2.ExecuteScalar()??0;
//如果((int)returnVal>0)
//*************************************************************
//编辑更新
//*************************************************************
//如果查询返回空值,通常不能使用整数
//保留null的值,因此我们使用可为null的类型
//(int?)将查询结果强制转换为int的null
Nullable returnVal=(int?)cmd2.ExecuteScalar();
//现在我们可以使用.GetValueOrDefault返回值
//如果int的默认值不为null(即0)
int bookCount=returnVal.GetValueOrDefault();
//此时bookCount应该是一个真正的整数-无需强制转换
如果(账面数>0)
//**************************************************************
//结束编辑更新
//**************************************************************
{
使用(SqlCommand cmd=new-SqlCommand(“插入到发行的书籍值(@SearchMembers等),连接))
{
//像上面的示例一样设置此命令的参数
cmd.Parameters.Add(“@SearchMembers”,SqlDbType.VarChar,100).Value=TextBoxSearchMembers.Text;
cmd.ExecuteNonQuery();
}
使用(SqlCommand cmd1=new-SqlCommand(“更新书籍\列表集BookAvailability=BookAvailability-1,其中BookName=@BoxBookName;”,连接))
{
cmd1.Parameters.Add(“@BoxBookName”,SqlDbType.VarChar,100);
cmd1.ExecuteOnQuery();
}
MessageBox.Show(“成功”);
这个。关闭();
}
其他的
{
MessageBox.Show(“书本不可用”);
}
}
}
}
捕获(异常exc)
{
Show(exc.ToString());
}
}
是否定义了dr2[“book_qty”]?当您输入if语句时,变量book_qty的值是多少?(如果您在if语句的行上放置一个brekpoint,visual studio中的调试器将告诉您这一点)除了Jakub Judas,你可能想做book\u qty+=
而不是book\u qty=
,或者在你的选择中使用SUM(book\u qty)
而不是*
。好吧,我把断点放在“if”行上,结果是book\u qty=0,没有得到任何值,所以程序只是跳到“else message”.我似乎找不到检查“BookAvailability”(数据类型为I)的方法
book_qty = Convert.ToInt32(dr2["BookAvailability"].ToString());
private void OPCode()
{
try
{
//keep your connections close to the vest (local)
using (SqlConnection connection = new SqlConnection())
//a using block ensures that your objects are closed and disposed
//even if there is an error
{
using (SqlCommand cmd2 = new SqlCommand("SELECT BookAvailability FROM Book_list WHERE BookName = @BookName", connection))
{
//Always use parameters to protect from sql injection
//Also it is easier than fooling with the single quotes etc.
//If you are referring to a TextBox you need to provide what property is
//being accessed. I am not in a WPF right now and not sure if .Text
//is correct; may be .Content
//You need to check your database for correct data type and field size
cmd2.Parameters.Add("@BookName", SqlDbType.VarChar, 100).Value = TextBoxBookName.Text;
//A select statement is not a non-query
//You don't appear to be using the data table or data adapter
//so dump them extra objects just slow things dowm
connection.Open();
//Comment out the next 2 lines and replaced with
//Edit Update
//var returnVal = cmd2.ExecuteScalar() ?? 0;
//if ((int)returnVal > 0)
//*************************************************************
//Edit Update
//*************************************************************
//in case the query returns a null, normally an integer cannot
//hold the value of null so we use nullable types
// the (int?) casts the result of the query to Nullable of int
Nullable<int> returnVal = (int?)cmd2.ExecuteScalar();
//now we can use the .GetValueOrDefault to return the value
//if it is not null of the default value of the int (Which is 0)
int bookCount = returnVal.GetValueOrDefault();
//at this point bookCount should be a real int - no cast necessary
if (bookCount > 0)
//**************************************************************
//End Edit Update
//**************************************************************
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO issue_book VALUES(@SearchMembers etc", connection))
{
//set up the parameters for this command just like the sample above
cmd.Parameters.Add("@SearchMembers", SqlDbType.VarChar, 100).Value = TextBoxSearchMembers.Text;
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd1 = new SqlCommand("UPDATE Book_list SET BookAvailability = BookAvailability-1 WHERE BookName = @BoxBookName;", connection))
{
cmd1.Parameters.Add("@BoxBookName", SqlDbType.VarChar, 100);
cmd1.ExecuteNonQuery();
}
MessageBox.Show("success");
this.Close();
}
else
{
MessageBox.Show("Book not available");
}
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}