Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在sql数据库表的一列中插入多个值_C#_Sql_Asp.net_Database - Fatal编程技术网

C# 在sql数据库表的一列中插入多个值

C# 在sql数据库表的一列中插入多个值,c#,sql,asp.net,database,C#,Sql,Asp.net,Database,我想知道是否可以向特定的数据库表单元格添加值 假设我有一个现有的数据库表,并且我向行中的特定列添加了一个新值,那么如何在不覆盖现有列的行的情况下向新值的列添加 我在谷歌上搜索,找到了这个方法 “插入用户(电话)值('9999975')” 但它给出了一个错误 无法将值NULL插入列“cardID”,列不允许NULL。插入失败 这是我的代码: protected void btnInsert_Click(object sender, EventArgs e) { try {

我想知道是否可以向特定的数据库表单元格添加值

假设我有一个现有的数据库表,并且我向行中的特定列添加了一个新值,那么如何在不覆盖现有列的行的情况下向新值的列添加

我在谷歌上搜索,找到了这个方法

“插入用户(电话)值('9999975')”

但它给出了一个错误

无法将值NULL插入列“cardID”,列不允许NULL。插入失败

这是我的代码:

protected void btnInsert_Click(object sender, EventArgs e)
{

    try
    {
        SqlConnection c = new SqlConnection();
        c.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
        string s = "INSERT INTO Users ( phone ) VALUES('99999975')";

        SqlCommand sqlcom = new SqlCommand(s, c);

        //sqlcom.Parameters.AddWithValue("@phone", TextNum.Text);

        c.Open();
        SqlDataReader read = sqlcom.ExecuteReader();
        while (read.Read())
        {


            Label3.Text += "name : " + read["name"].ToString() + "<br/>";//start with +=
            Label3.Text += "password: " + read["password"].ToString() + "<br/>";
            Label3.Text += "phone : " + read["phone"].ToString() + "<br/>";
            Label3.Text += "email : " + read["email"].ToString() + "<br/><br/>";
            Label3.Text += "cardID : " + read["cardID"].ToString() + "<br/><br/>";

        }
        //sqlcom.ExecuteNonQuery();
        Label3.Text = "Insert successful";
        read.Close();
        //createTable();
        c.Close();
    }
    catch (Exception ee)
    {
        Label3.Text = ee.Message;
    }
}
protectedvoid btn插入\u单击(对象发送方,事件参数e)
{
尝试
{
SqlConnection c=新的SqlConnection();
c、 ConnectionString=@“数据源=。\SQLEXPRESS;AttachDbFilename=|DataDirectory | \Database.mdf;集成安全性=True;用户实例=True”;
string s=“插入用户(电话)值('9999975')”;
SqlCommand sqlcom=新的SqlCommand(s,c);
//sqlcom.Parameters.AddWithValue(“@phone”,TextNum.Text);
c、 Open();
SqlDataReader read=sqlcom.ExecuteReader();
while(read.read())
{
Label3.Text+=“name:”+读取[“name”]。ToString()+“
”;//以开头+= Label3.Text+=“密码:”+read[“password”].ToString()+“
”; Label3.Text+=“phone:”+读取[“phone”].ToString()+“
”; Label3.Text+=“email:”+读取[“email”].ToString()+“

”; Label3.Text+=“cardd:”+读取[“cardd”]。ToString()+“

”; } //sqlcom.ExecuteNonQuery(); Label3.Text=“插入成功”; read.Close(); //createTable(); c、 Close(); } 捕获(异常ee) { Label3.Text=ee.Message; } }

任何帮助都将不胜感激

服务器的响应非常清楚:

无法将值NULL插入列'cardd',列不插入 不允许空值。插入失败

您必须将查询转换为

  string s = 
    @"INSERT INTO Users ( 
        phone,
        cardId)    -- you have to insert into this column
      VALUES(
        '99999975',
        '12346789') --todo: put actual CardId here";


似乎您的表要求“cardd”为非null,您可以更改表以接受该字段的null值,或者为“cardd”传递一个空字符串

您应该评估是否有一个空的cardID是可以的,或者是否可能导致错误,因为目前它是一个必填字段,我想这是有原因的

编辑: 如果您想更改现有电话号码(而不是添加新行),则应使用更新查询

插入添加新行


UPDATE允许您修改一行或多行

您的表将
cardID
作为主键,并且根据定义,它不能为空。 插入新行时,必须为每个不可为空的列设置一个值。在您的例子中,
cardd
列就是其中之一,但它可能不是唯一一个

有几种方法可以插入要插入的数据并避免该消息。 完成所需操作的一种方法是通过执行以下命令为主键设置一个值:

插入用户(cardId、phone)值(1234,'99999')

另一种方法是将Users表的主键设置为
identity
,这样,它将获得自己的自动生成id,而您不需要设置它的值

 CREATE TABLE Users
 (
   cardID int identity(1,1),
   phone varchar(10)
 );

insert into users(phone) values('99999967');

INSERT将新记录添加到表中。它不会更新现有记录。在任何情况下,如果您的表中有一个名为cardd的列,该列被标记为不接受空值,那么您必须为该列提供一个值—可能您希望进行更新而不是插入—这绝对是您想要的。在[here.][1][1]获取战利品:此外,在遵循@e4c5发布的链接中的建议后,您应该重新回顾您对ADO.NET的了解。同时由ExecuteReader执行的更新或插入查询不会返回有效的数据读取器,因此ExecuteReader之后的代码不会按预期执行。您应该调用ExecuteOnQuery来插入或更新,然后提供一个adeguate SELECT语句来读取存储的值。
 CREATE TABLE Users
 (
   cardID int identity(1,1),
   phone varchar(10)
 );

insert into users(phone) values('99999967');