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

春春丸

我对这段代码有问题......我对发生的事情一无所知......

当我在 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();

提前致谢

马哈茂德
        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值,如果该列在数据库中可以为空,DBNull.Value则将返回a 所以你应该在处理它之前检查这个列是否有这样的值
此外,你不应该接受来自用户输入的值并将它们直接注入到 SQL 查询中!ADO.Net. Sql Parameters的属性Parameters可以找到称为它的东西SqlCommand您应该使用此属性将来自用户的值作为参数添加到查询中
例如,可以像这样添加客户端名称:

    com.Parameters.Add(new SqlParameter("client_name", comboBox1.Text));

现在您告诉 Sql 命令该值实际上是在SqlParameters集合中显示的,如下所示:

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

看看这个问题为什么我们总是喜欢在 SQL 语句中使用参数?
这是属性MSDN 参考Parameters

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C#将数据从数据表插入SQL Server数据库

C#-将包含关系数据表的数据集插入到SQL Server中

将xml中的数据提取到SQL server表中

实体框架 - 将记录列表从数据表保存到 SQL Server 数据库表

如何使用SQL Server替换数据表中的名称

SQL Server数据库将“ć”存储为“ c”

归档SQL Server数据表数据?

使用C#将SQL Server 2008中的多个SQL查询合并到ASP.NET上的数据表中

将数据从SQL Server数据库导入HTML表

如何从数据表vb.net向SQL Server数据库插入值

无法从C#中的SQL Server数据库读取

C#无法将数据插入SQL Server数据库

无法使用C#将数据导入SQL Server数据库

将数据从 SQL Server 数据库加载到 C#

我可以在SQL Server和C#数据表的列名中使用特殊字符吗

如何使用c#将文本文件中的某些数据加载到sql server数据库中

如何在SQL Server的数据表中动态添加行

SQL SERVER中如何通过存储过程插入数据表

无法从C#将inkPicture插入SQL Server数据库

c#如何将多个文件上传到SQL Server数据库?

将数据从Microsoft Word提取到数据库表

计算SQL Server数据库中的表数

在 SQL Server 数据库表中存储 TTreeView

我想隐藏SQL Server数据库中的表

将 XML 文件插入 SQL Server 中的数据库

将C#数据表批量插入到SQL Server-有效检查重复项

与使用三个SQL查询将信息从数据库中提取到html列中发生冲突

如何在C#中解析通用XML文件以在SQL Server数据库中创建表

使用C#还原SQL Server数据库