代码之家  ›  专栏  ›  技术社区  ›  Darren

检索int数据库并将其存储在C中的变量中#

  •  0
  • Darren  · 技术社区  · 7 年前

    我是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
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Romano Zumbé Ajay Jain    7 年前

    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);
    }
    

    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);
    }
    
        2
  •  0
  •   Ortund    7 年前

    int FinalDays = int.TryParse(dt.Rows[0][0], out FinalDays) ? FinalDays : 0;
    

    这是一种“语法糖”,它试图将数据表列中的值(即从数据库中获得的值)解析为整数,并将解析后的整数值输出到 FinalDays 。如果解析有效(即值实际上是一个数字),则FinalDays将设置为数据库中的值,如果解析无效,则FinalDays将设置为0。

        3
  •  0
  •   Darren    7 年前

    嗨,伙计们,我找到了另一个适合我的选择……希望它能帮助那些遇到和我一样困难的人

            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();