Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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# 如何更改数据库中列的值_C#_Asp.net_Localdb - Fatal编程技术网

C# 如何更改数据库中列的值

C# 如何更改数据库中列的值,c#,asp.net,localdb,C#,Asp.net,Localdb,我正在为幸运抽奖系统的注册页面工作。我已经创建了一个包含emp_id、emp_名称、emp_部门和考勤的员工数据库。我已将考勤栏设置为默认值“缺席”。但数据库上的显示为空。单击此链接可查看 因此,注册过程是用户需要使用usb条形码扫描仪扫描他们的条形码,然后系统将显示他们的姓名,并将考勤栏上的默认值更新为“出席”。我卡在了该部分。有人能给我一些想法/解决方案吗 考勤代码: using System; using System.Collections.Generic; using System.

我正在为幸运抽奖系统的注册页面工作。我已经创建了一个包含emp_id、emp_名称、emp_部门和考勤的员工数据库。我已将考勤栏设置为默认值“缺席”。但数据库上的显示为空。单击此链接可查看

因此,注册过程是用户需要使用usb条形码扫描仪扫描他们的条形码,然后系统将显示他们的姓名,并将考勤栏上的默认值更新为“出席”。我卡在了该部分。有人能给我一些想法/解决方案吗

考勤代码:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Attendance : System.Web.UI.Page
{
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string str;
    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = @"Data Source= (LocalDB)\MSSQLLocalDB; AttachDbFilename = 
           C:\Users\Nor  Asyraf  Mohd No\source\repos\LuckyDraw\LuckyDraw\App_Data\ticket.mdf; 
            Integrated Security = True";

        con.Open();
        txtText.Focus();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {



    }

void idcheck()
        {
            string selectQuery = "SELECT count(*) FROM EMPLOYEES where EMP_ID = '" + txtText.Text + "'";
            SqlCommand cmd = new SqlCommand(selectQuery, con);
            SqlDataReader myReader = null;
            int count = Convert.ToInt32(cmd.ExecuteScalar());
            if (count > 0)

            {
                myReader = cmd.ExecuteReader();
                while (myReader.Read())
                    lblError.Text = myReader["EMP_NAME"].ToString();
                lblError.ForeColor = System.Drawing.Color.Green;

            }
            else
            {
                lblError.Text = "Employee not available";
                lblError.ForeColor = System.Drawing.Color.Red;
            }

        }

}

如果我正确理解您的问题,您希望在btnSave_Click函数中更新Attendance to'Present'的值。我认为您应该能够使用以下代码来实现这一点:

protected void btnSave_Click(object sender, EventArgs e)
{
    using (SqlConnection connection = new SqlConnection(connectionString)) {
        using (SqlCommand command = connection.CreateCommand()) {
            command.Text = "UPDATE EMPLOYEES SET Attendance = 'Present' WHERE EMP_ID = @id";
            command.Parameters.AddWithValue("@id", txtText.Text);
            connection.Open();

            command.ExecuteNonQuery();

            connection.Close();
        }
    }
}
使用command.Parameters.AddWithValue的好处是它有助于防止SqlInjections


using语句确保在代码离开using块后释放连接。您可能也希望在其他函数中使用它们,如果不这样做,则可能是因为您有多个到同一数据库的并发连接,所以上面的代码引发了一个异常

您的问题不清楚,但要设置列的默认值,请转到SQL Management studio并执行以下操作:

  • 右键单击您的表
  • 选择设计
  • 选择所需列,在本例中为“考勤”
  • 更改“默认值或绑定”属性

  • 您能以DB为单位显示该列的默认值吗?现在该列的默认值为null@Coding[我认为您的解决方案会起作用,但我的id检查代码仍然存在问题,因此我还无法运行系统@user2074945如何检查数据库上的emp_id并根据已扫描的id显示emp_名称?以下是我尝试过的代码。
    public void idcheck(){string query=“从EMP_ID='”+txtText.Text+“'”;SqlDataAdapter ada=new SqlDataAdapter(query,con);DataTable dt=new DataTable();ada.Fill(dt);if(dt.Rows.Count>0){lblError.Text=dt.Rows[0][“EMP_NAME”].ToString();}
    您的代码到底有什么问题?错误发生在哪里?错误消息是什么?现在我的id检查有问题,我无法在数据库中检查id并在标签上显示员工姓名。