如何根据leavetype和emp id在textbox1中显示emp离开余额 [英] how to show emp leave balance in textbox1 based on leavetype and emp id

查看:140
本文介绍了如何根据leavetype和emp id在textbox1中显示emp离开余额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据leavetype和emp id在textbox1中显示emp leave balance。我正在使用下面的代码,但它没有在文本框中显示总休假余额。

请帮助。



private void cbleavetype_SelectedIndexChanged()

{

conn = new SqlConnection(@Data Source = ........)

conn.Open();

SqlCommand command = new SqlCommand(select leave leave from leaveallotment where id ='+ comboBox1.Text +'and leavetype ='+ cbleavetype.Text +',conn);

SqlDataAdapter adp = new SqlDataAdapter(command);

DataTable tbl = new DataTable();

adp.Fill(tbl);

textBox1.Text = cbleavetype.SelectedValue.ToString();

}

解决方案

更简单的方法是不使用Sql Adapter。



做这样的事情:

 对象 temp = cmd.ExecuteScalar();  //  这将返回select语句的第一个值。 
if (temp!= null
textBox1.Text = temp.ToString();





此外,您还需要使用参数。使用您的代码,现在有人可以进行SQL注入并损坏您的数据库。



而是:

 SqlCommand command =  new  SqlCommand( 选择从leaveallotment离开,其中id = @id和leavetype = @leavetype,conn); 
cmd.Parameters.AddWithValue( @ id,combBox1.Text);
cmd.Parameters.AddWithValue( @ leavetype,cbleavetype.Text);
... // 然后如上所述调用ExecuteScalar()


I want to show emp leave balance in textbox1 based on leavetype and emp id. I'm using below code but it's not showing total leave balance in textbox.
please help.

private void cbleavetype_SelectedIndexChanged()
{
conn = new SqlConnection(@"Data Source=........")
conn.Open();
SqlCommand command = new SqlCommand("select leave from leaveallotment where id='" + comboBox1.Text + "' and leavetype='" + cbleavetype.Text + "'",conn);
SqlDataAdapter adp = new SqlDataAdapter(command);
DataTable tbl = new DataTable();
adp.Fill(tbl);
textBox1.Text = cbleavetype.SelectedValue.ToString();
}

解决方案

A much easier way is to not use the Sql Adapter.

Do something like this:

Object temp = cmd.ExecuteScalar();  // this returns the first value of the select statement.
if (temp != null)
  textBox1.Text = temp.ToString();



Also, you'll want to use parameters. With your code the way it is now someone can do sql injections and do damage to your database.

Instead do:

SqlCommand command = new SqlCommand("select leave from leaveallotment where id=@id and leavetype= @leavetype",conn);
cmd.Parameters.AddWithValue("@id", combBox1.Text);
cmd.Parameters.AddWithValue("@leavetype", cbleavetype.Text);
...  // then call ExecuteScalar() as mentioned above


这篇关于如何根据leavetype和emp id在textbox1中显示emp离开余额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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