如果数据库中存在的书数为0,则显示错误消息,否则如果值为5则逐个递减计数器 [英] If number of book present in the database is 0 then show error message otherwise if value is 5 then decrement the counter one by one

查看:92
本文介绍了如果数据库中存在的书数为0,则显示错误消息,否则如果值为5则逐个递减计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置数据库中的书籍数量时遇到问题是0然后显示No Book Is Available消息,否则如果值为5则在窗口应用程序中逐个递减计数器。





请尽快回复我..紧急



我尝试了什么:



private void btnSave_Click(object sender,EventArgs e)

{



textBox1.Text = getuniqueID();

SqlConnection con = new SqlConnection(Main.connectionname());



SqlCommand cmd = new SqlCommand();









string quiry1 = 从BookEntry中选择NoOfBook,其中AccessionNo ='+ comboBox2.Text +'和NoOfBook< = 0;





MessageBox .Show(Book is not available for issue,Input Error,MessageBoxButtons.OK,Message BoxIcon.Error);







string quiry =BEGIN TRY+

BEGIN TRANSACTION+

INSERT INTO [BookIssue]([T_Id],[issue_date],[due_date],[S_Id],[S_Name],[course],[department] ,[Accession_no],[book_title],[作者],[ISBN],[版本])+

VALUES(@ T_Id,@ issue_date,@ due_date,@ S_Id,@ S_Name,@当然,@ department,@ Accession_no,@ book_title,@ author,@ ISBN,@ edition);









string update =UPDATE [BookEntry] SET [NoOfBook] = [NoOfBook] - 1 WHERE AccessionNo ='+ comboBox2.Text +'和NoOfBook> 0; < br $>


cmd.Parameters.AddWithValue(@ T_Id,textBox1.Text.Trim());

cmd.Parameters.AddWithValue( @issue_date,dateTimePicker1.Text.Trim());

cmd.Parameters.AddWithValue(@ due_date,dateTimePicker1 .Text.Trim());

cmd.Parameters.AddWithValue(@ S_Id,comboBox1.Text.Trim());

cmd.Parameters.AddWithValue( @S_Name,textBox3.Text.Trim());

cmd.Parameters.AddWithValue(@ course,textBox4.Text.Trim());

cmd.Parameters.AddWithValue(@ department,textBox5.Text.Trim());

cmd.Parameters.AddWithValue(@ Accession_no,comboBox2.Text.Trim());

cmd.Parameters.AddWithValue(@ book_title,textBox7.Text.Trim());

cmd.Parameters.AddWithValue(@ author,textBox8.Text.Trim ());

cmd.Parameters.AddWithValue(@ ISBN,textBox9.Text.Trim());

cmd.Parameters.AddWithValue(@ edition ,textBox10.Text.Trim());





quiry + = update;



quiry + =COMMIT TRANSACTION+

结束尝试+

BEGIN CATCH+

ROLLBACK TRANSACTION+

END CATCH;



cmd.Connection = con;

cmd.CommandType = CommandType.Text;

cmd.CommandText = quiry;



尝试

{

con.Open(); //在App和DB之间建立phy链接

cmd.ExecuteNonQuery();

MessageBox.Show(信息已保存。,库应用程序,MessageBoxButtons.OK,MessageBoxIcon.Information);

< br $>
}



catch(SqlException ex)

{



MessageBox.Show(ex.Message);

}



最后

{

if(con.State == ConnectionState.Open)

{

con.Close( );

}

}

}

i am facing problem in setting number of book present in the database is 0 then show "No Book Is Available " message otherwise if value is 5 then decrement the counter one by one in window application.


Please Reply me soon.. Its urgent

What I have tried:

private void btnSave_Click(object sender, EventArgs e)
{

textBox1.Text = getuniqueID();
SqlConnection con = new SqlConnection(Main.connectionname());

SqlCommand cmd = new SqlCommand();




string quiry1 = "select NoOfBook from BookEntry where AccessionNo='" + comboBox2.Text + "' and NoOfBook <=0";


MessageBox.Show("Book is not available for issue", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);



string quiry = "BEGIN TRY " +
"BEGIN TRANSACTION " +
"INSERT INTO [BookIssue]([T_Id],[issue_date],[due_date],[S_Id],[S_Name],[course],[department],[Accession_no],[book_title],[author],[ISBN],[edition])" +
"VALUES(@T_Id,@issue_date,@due_date,@S_Id,@S_Name,@course,@department,@Accession_no,@book_title,@author,@ISBN,@edition)";




string update = "UPDATE [BookEntry] SET [NoOfBook] = [NoOfBook] - 1 WHERE AccessionNo = '" + comboBox2.Text + "' And NoOfBook >0";

cmd.Parameters.AddWithValue("@T_Id", textBox1.Text.Trim());
cmd.Parameters.AddWithValue("@issue_date", dateTimePicker1.Text.Trim());
cmd.Parameters.AddWithValue("@due_date", dateTimePicker1.Text.Trim());
cmd.Parameters.AddWithValue("@S_Id", comboBox1.Text.Trim());
cmd.Parameters.AddWithValue("@S_Name", textBox3.Text.Trim());
cmd.Parameters.AddWithValue("@course", textBox4.Text.Trim());
cmd.Parameters.AddWithValue("@department", textBox5.Text.Trim());
cmd.Parameters.AddWithValue("@Accession_no", comboBox2.Text.Trim());
cmd.Parameters.AddWithValue("@book_title", textBox7.Text.Trim());
cmd.Parameters.AddWithValue("@author", textBox8.Text.Trim());
cmd.Parameters.AddWithValue("@ISBN", textBox9.Text.Trim());
cmd.Parameters.AddWithValue("@edition", textBox10.Text.Trim());


quiry += update;

quiry += " COMMIT TRANSACTION " +
"END TRY " +
"BEGIN CATCH " +
"ROLLBACK TRANSACTION " +
" END CATCH";

cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = quiry;

try
{
con.Open();//establishes phy link between App and DB
cmd.ExecuteNonQuery();
MessageBox.Show("Information Saved.", "Library Application", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

catch (SqlException ex)
{

MessageBox.Show(ex.Message);
}

finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}

推荐答案

根据你的代码你提供的,你只有定义和声明query1但从未在sqlcommand中使用它来执行。如果你没有执行任何查询,那么你怎么能得到比较的结果。

此外,如果你为messagebox编写了query1,那么该查询也是不正确的。您的查询应如下所示:



As per your code which you have provided, you only have define and declare query1 but never used it in sqlcommand to execute. If you are not executing any query then how can you get result to compare.
Also if you have written query1 for messagebox then that query also is not proper. Your query should be like below,

string quiry1 = "select COUNT(*) from BookEntry where AccessionNo='" + comboBox2.Text + "' and NoOfBook <=0";







SqlCommand cmd=new SqlCommand(query1, con);
SqlDataReader dr1 ;
dr1=cmd.ExecuteReader();
if (cmd.ExecuteReader().RecordsAffected > 0)
{
    if ((int)cmd.ExecuteScalar()<=0)
    {
        MessageBox.Show("Book is not available for issue", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
cmd.ExecuteReader().Close();
cmd.Dispose();


这篇关于如果数据库中存在的书数为0,则显示错误消息,否则如果值为5则逐个递减计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆