c#将数据库提取到SQL Server数据表中

时间:2017-12-09 08:37:27

标签: c# sql-server ado.net

我对这段代码有疑问......我对发生的事情没有丝毫的线索......

当我在Visual Studio中运行此代码时,我收到错误消息:

  

System.InvalidCastException:'无法将对象从DBNull强制转换为其他类型。'

这是代码:

conn.Open();

SqlCommand com = new SqlCommand("update lend set date_back=convert(datetime2, getdate(), 102) where client_name ='" + comboBox1.Text + "'", conn);

SqlDataAdapter da = new SqlDataAdapter("Declare @startdate smalldatetime declare @enddate smalldatetime set @startdate = (select date_lended from dbo.lend where client_name = '" + comboBox1.Text + "') set @enddate = (select date_back from dbo.lend where client_name = '" + comboBox1.Text + "') SELECT DATEDIFF(DAY, @startdate+2, @enddate)as timepassedd", conn);

DataTable dt = new DataTable();
da.Fill(dt);

foreach (DataRow DR in dt.Rows)
{
    int date;
    date = Convert.ToInt32(DR["timepassedd"]);

    if (date > 0)
    {
        com = new SqlCommand("DELETE lend WHERE client_name ='"+comboBox1.Text+"'" +
                             "UPDATE book_list set book_stock = book_stock  1 WHERE book_name ='" + comboBox1.Text + "'",conn);
        com.ExecuteNonQuery();

        MessageBox.Show("You Returned the book " + date + " Days Late!" +
                            "please pay the fee to the front desk");

        UserPanel u = new UserPanel();
        u.Show();

        this.Hide();
    }
    else if (date <= 0)
    {
        com = new SqlCommand("DELETE lend WHERE client_name ='" + comboBox1.Text + "'" +
                             "UPDATE book_list set book_stock = book_stock  1 WHERE book_name ='" + comboBox1.Text + "'", conn);
        com.ExecuteNonQuery();

        MessageBox.Show("You returned the book " + date + " Days Late!" +
                            "please pay the fee to the front desk");

        UserPanel u = new UserPanel();
        u.Show();

        this.Hide();
    }
}

conn.Close();

提前致谢

3 个答案:

答案 0 :(得分:0)

在答案下方运行代码将是显而易见的:

                string sql1 = "update lend set date_back=convert(datetime2, getdate(), 102) where client_name ='" + comboBox1.Text + "'";
                Console.WriteLine(sql1);
                Console.ReadLine();
                SqlCommand com = new SqlCommand(sql1, conn);
                string sql2 = "Declare @startdate smalldatetime declare @enddate smalldatetime set @startdate = (select date_lended from dbo.lend where client_name = '" + comboBox1.Text + "') set @enddate = (select date_back from dbo.lend where client_name = '" + comboBox1.Text + "') SELECT DATEDIFF(DAY, @startdate+2, @enddate)as timepassedd"
                Console.ReadLine();
                SqlDataAdapter da = new SqlDataAdapter(sql2, conn);

答案 1 :(得分:0)

Child

您应该检查 conn.Open(); SqlCommand com = new SqlCommand("update lend set date_back=convert(datetime2, getdate(), 102) where client_name ='" + comboBox1.Text + "'", conn); SqlDataAdapter da = new SqlDataAdapter("Declare @startdate smalldatetime declare @enddate smalldatetime set @startdate = (select date_lended from dbo.lend where client_name = '" + comboBox1.Text + "') set @enddate = (select date_back from dbo.lend where client_name = '" + comboBox1.Text + "') SELECT DATEDIFF(DAY, @startdate+2, @enddate)as timepassedd", conn); DataSet ds = new DataSet(); da.Fill(ds); if(ds.Tables.Count == 1) { if(ds.Tables[0].Rows.Count > 0) { foreach(DataRow dr in ds.Tables[0].Rows) { if(dr.ItemArray.Length > 0) { if(dr["timepassedd"] != DBNull.Value) { int date; date = Convert.ToInt32(Dr["timepassedd"]); if (date > 0) { com = new SqlCommand("delete lend where client_name ='" + comboBox1.Text + "'" + "UPDATE book_list set book_stock = book_stock 1 WHERE book_name ='" + comboBox1.Text + "'", conn); com.ExecuteNonQuery(); MessageBox.Show("You Returned the book " + date + " Days Late!" + "please pay the fee to the front desk"); UserPanel u = new UserPanel(); u.Show(); this.Hide(); } else if (date <= 0) { com = new SqlCommand("delete lend where client_name ='" + comboBox1.Text + "'" + "UPDATE book_list set book_stock = book_stock 1 WHERE book_name ='" + comboBox1.Text + "'", conn); com.ExecuteNonQuery(); MessageBox.Show("You Returned the book " + date + " Days Late!" + "please pay the fee to the front desk"); UserPanel u = new UserPanel(); u.Show(); this.Hide(); } } } } } } conn.Close(); 值,如果数据库中的列无效,则会返回DBNull!因此,在处理之前,您应该检查此列是否有这样的值 此外,您不应该接受来自用户输入的值并直接将它们注入SQL查询中! DBNull.Value。有一个名为ADO.Net的内容可以在Sql Parameters类的属性Parameters中找到。您应该使用此属性将用户的值作为参数添加到查询中 例如,客户端名称可以像这样添加:

SqlCommand

现在告诉Sql Command这个值实际上是在 com.Parameters.Add(new SqlParameter("client_name", comboBox1.Text)); 集合中显示的,如下所示:

SqlParameters

看一下这个问题Why do we always prefer using parameters in the SQL statements?
这是SqlCommand com = new SqlCommand("update lend set date_back=convert(datetime2, getdate(), 102) where client_name ='@client_name'", conn); 属性

MSDN reference

答案 2 :(得分:0)

set @startdate = (select date_lended from dbo.lend where client_name = '" + comboBox1.Text + "')
set @enddate = (select date_back from dbo.lend where client_name = '" + comboBox1.Text + "')

a)如果comboBox1.Text曾经包含'characters

,则容易受到意外行为的影响

b)如果没有与组合框中所选内容匹配的记录,则两者都将导致@startdate或@enddate为NULL,然后为'@datepassedd'生成NULL值,因此在尝试转换为int时出错

c)你不需要两次查询数据库,你可以做到

select DATEDIFF(DAY, date_lended+2, date_back) as timepassedd FROM dbo.lend where client_name = @clientName

(@ clientName可以添加为参数)

在这种情况下,如果没有与名称匹配的记录,则不会返回任何行。顺便说一下,从来没有必要使用DataTable并遍历记录,因为现在查询只返回一行。