Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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#_Visual Studio 2019 - Fatal编程技术网

C# 如何将客户数据添加到SQL数据库中?

C# 如何将客户数据添加到SQL数据库中?,c#,visual-studio-2019,C#,Visual Studio 2019,这是我的CustomerRegister类,但我似乎无法将数据从我的addressTextBox输入CustomerDBL DataBase dbObj = new DataBase(); string selStr = "Update CustomerTbl Set customer_address = '" + addressTextBox.Text + "' Where custID = " + "NULL"; int i =

这是我的CustomerRegister类,但我似乎无法将数据从我的addressTextBox输入CustomerDBL

DataBase dbObj = new DataBase();

string selStr = "Update CustomerTbl Set customer_address = '" + addressTextBox.Text + "' Where custID = " + "NULL";
 int i = dbObj.ExecuteNonQuery(selStr);
这是我的数据库类,但
返回comdObj.ExecuteNonQuery()不起作用,因为没有名为NULL的custID。那么,我如何以这种方式编程,以便在新用户注册时能够不断更新数据库呢

class DataBase
    {
        string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\OOPG\Banking Mini Project Raynard\Banking Mini Project Raynard\Database1.mdf;Integrated Security = True";
        SqlConnection connObj;
        SqlCommand comdObj;
        SqlDataReader dR;

        public DataBase()
        {
            connObj = new SqlConnection(connStr);
            connObj.Open();
        }
        public SqlDataReader ExecuteReader(string selStr)
        {
            comdObj = new SqlCommand(selStr, connObj);
            dR = comdObj.ExecuteReader();
            return dR;
        }
        public int ExecuteNonQuery(string sqlStr)
        {
            comdObj = new SqlCommand(sqlStr, connObj);
            return comdObj.ExecuteNonQuery();
        }
    }
如果要添加记录,则需要
插入
,而不是
更新
。例如(这里使用来完成所有繁重的工作,包括参数处理):

使用简洁;
//...
无效UpsertAddress(int?id,字符串地址)
{
如果(id为空)
{
connection.Execute(“插入CustomerTbl(客户地址)值(@address);”,
new{address});//可能使用OUTPUT子句获取标识
}
其他的
{
连接。执行(
“更新CustomerTbl设置客户地址=@address,其中custID=@id;”,
新的{id,address});
}
}
如果要添加记录,则需要
插入
,而不是
更新
。例如(这里使用来完成所有繁重的工作,包括参数处理):

使用简洁;
//...
无效UpsertAddress(int?id,字符串地址)
{
如果(id为空)
{
connection.Execute(“插入CustomerTbl(客户地址)值(@address);”,
new{address});//可能使用OUTPUT子句获取标识
}
其他的
{
连接。执行(
“更新CustomerTbl设置客户地址=@address,其中custID=@id;”,
新的{id,address});
}
}

在执行任何查询之前,首先应该创建到SQL数据库的连接。之后,您应该能够在将任何数据更新到数据库之前插入数据。成功插入数据后,可以使用上述命令文本更新数据。下面是一些用于插入用于注册客户的数据的示例代码

using (SqlCommand command = new SqlCommand())
{
    command.Connection = connection;            // <== lacking
    command.CommandType = CommandType.Text;
    command.CommandText = "INSERT into CustomerTbl (CustId, Name, Address) VALUES (@CustId, @Name, @Address)";
    command.Parameters.AddWithValue("@CustId", name);
    command.Parameters.AddWithValue("@Name", userId);
    command.Parameters.AddWithValue("@Address", idDepart);

    try
    {
        connection.Open();
        int recordsAffected = command.ExecuteNonQuery();
    }
    catch(SqlException)
    {
        // error here
    }
    finally
    {
        connection.Close();
    }
}
使用(SqlCommand=newsqlcommand())
{

command.Connection=Connection;//首先,您应该在执行任何查询之前创建到SQL数据库的连接。之后,您应该能够在将任何数据更新到数据库之前插入数据。成功插入数据后,您可以使用上述命令文本更新数据。下面是一些用于插入regi数据的示例代码给顾客消毒

using (SqlCommand command = new SqlCommand())
{
    command.Connection = connection;            // <== lacking
    command.CommandType = CommandType.Text;
    command.CommandText = "INSERT into CustomerTbl (CustId, Name, Address) VALUES (@CustId, @Name, @Address)";
    command.Parameters.AddWithValue("@CustId", name);
    command.Parameters.AddWithValue("@Name", userId);
    command.Parameters.AddWithValue("@Address", idDepart);

    try
    {
        connection.Open();
        int recordsAffected = command.ExecuteNonQuery();
    }
    catch(SqlException)
    {
        // error here
    }
    finally
    {
        connection.Close();
    }
}
使用(SqlCommand=newsqlcommand())
{

command.Connection=Connection;//重要提示:您的SQL方法适合SQL注入。您不应该将用户输入串联起来创建SQL(这对i18n/l10n和其他原因也不好)。始终:使用参数。这非常重要(否则,有人可能会破坏或泄露你的数据;老实说,这是破解应用程序最简单的方法)除了明显的sql注入弱点之外:实际上,您不应该尝试重用连接、命令和其他ado.net实例。创建、使用、处置通常被认为是最佳实践。有关如何使用sql参数的信息,请参阅。有关说明sql注入如何损害应用程序数据的有趣动画,请参阅。另请参阅重要信息:y我们的SQL方法对于SQL注入来说已经成熟了。你永远不应该连接用户输入来创建SQL(这对i18n/l10n和其他原因也不好)。始终:使用参数。这真的很重要(有人可能会破坏或泄漏你的数据,否则;老实说,这是破解应用程序最简单的方法)除了明显的sql注入弱点之外:您确实不应该尝试重用连接、命令和其他ado.net实例。创建、使用、处置通常被认为是最佳实践。有关如何使用sql参数的信息,请参阅。有关说明sql注入如何损害您的应用程序数据的有趣动画,请参阅说明:由于我们无法看到此连接的生存期,因此像这样使用
Open()
/
Close()
可能是不合适的(如果我们正在创建连接,那么是:调用
Open()
)次要观察:由于我们无法看到此连接的生存期,因此像这样使用
Open()
/
Close()
可能不合适(如果我们正在创建连接,那么是的:调用
Open()