检索int数据库并将其存储在一个变量中C#
问题描述:
我是新来的C#我想知道如何从数据库中检索一个整数值并将其存储在一个变量中。检索int数据库并将其存储在一个变量中C#
之后,我需要使用此值来添加另一个已完成的整数并将其存回数据库。
我已经搜索了很多方法,但大多数给出的方法将它存储在数据网格视图中,但我想在不显示的情况下进行计算。
SqlDataAdapter sql1 = new SqlDataAdapter("Select finaldays from LeaveTest where Id = '" + comboBox1.SelectedText + "'", con);//it will take from the selection of combobox
DataTable dt = new DataTable();
sql1.Fill(dt);
con.Open();
TimeSpan timespan1;
timespan1 = dateTimePicker3.Value - dateTimePicker2.Value;
int TotalDays = timespan1.Days+1;//total days that are taken from the datetimepicker
MessageBox.Show(TotalDays.ToString());//displaying the date for testing
答
嗨,大家好,我发现了另一个替代方案,为我工作...希望它可以帮助别人谁遇到同样的困难,因为我
con.Open();
string sqlSelectQuery = "Select * FROM LeaveTest";
SqlCommand cmd = new SqlCommand(sqlSelectQuery, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
TimeSpan timespan1;
timespan1 = dateTimePicker3.Value - dateTimePicker2.Value;
int totaldays = timespan1.Days;
textBox2.Text = (dr["finaldays"].ToString());
int finaldays = Convert.ToInt32(textBox2.Text)+totaldays;
//textbox2 is a temporary textbox(which is invisible)that is used to put the value finaldays in from the database
string sql1 = ("Update LeaveTest SET finaldays = '"+ finaldays + "' WHERE Id='" + comboBox1.Text + "'"); //updating the finaldays in database
SqlCommand cmd2 = new SqlCommand(sql1, con);
con.Close();
con.Open();
SqlDataReader dr2 =cmd2.ExecuteReader();
con.Close();
}
con.Close();
的代码会从天2日期时间pickers.The量从数据库中计算总天数(totaldays)将被检索并添加了总天数(finaldays)。我的解释可能有点不好,但我希望你们会受益于此代码。:)
答
为此使用SqlCommand
。如果您只希望检索一个值ExecuteScalar
是最好的选择。另外,您应该使用参数来防止SQL注入。
SqlCommand cmd = new SqlCommand("Select finaldays from LeaveTest where Id = @id", con);
cmd.Parameters.Add("@id", SqlDbType.VarChar);
cmd.Parameters["@id"].Value = comboBox1.SelectedText;
try
{
conn.Open();
int result = (Int32)cmd.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
如果有可能是每个ID多个结果,你应该使用一个SqlReader
和结果存储在List
(比如上例中),或者与他们的工作时,他们被检索:
SqlCommand cmd = new SqlCommand("Select finaldays from LeaveTest where Id = @id", con);
cmd.Parameters.Add("@id", SqlDbType.VarChar);
cmd.Parameters["@id"].Value = comboBox1.SelectedText;
try
{
List<int> resultList = new List<int>();
conn.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
resultList.Add((Int32)reader[0]);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
答
你重新走上正轨,但这真的是你可以在Google上轻松找到答案的地方......
现在你已经填充了你的DataTable,你只需要获得对列值的引用。
int FinalDays = int.TryParse(dt.Rows[0][0], out FinalDays) ? FinalDays : 0;
因为您是C#的新手,我会解释一下这是干什么的。
这是一个“语法糖”,它试图从DataTable列(即从数据库获得的值)解析整数值,并将解析后的整数值输出到FinalDays
。如果解析工作(即该值实际上是一个数字),则FinalDays将设置为数据库中的值,如果它不起作用,则FinalDays将设置为0.
我还没有添加vari能够为整数值,但让我们想要使用的变量是int = finaldays – Darren
[阅读此](https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using -parameters-in-sql-statements)当你得到一分钟 – musefan
用以下代码替换上面的代码。 SqlCommand com = new SqlCommand(“Select Leavedest from LeaveTest where Id ='”+ comboBox1.SelectedText +“'”,con); int x =(int)com.ExecuteScalar(); – Koderzzzz