gpt4 book ai didi

c# - 解决连接字符串问题

转载 作者:行者123 更新时间:2023-12-04 08:33:43 24 4
gpt4 key购买 nike

我有一个 SQL 连接字符串,它给了我一个错误。
错误是:
System.ArgumentException: '不支持关键字:'allowuservariables'。'

using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = "server=localhost;user id=root;database=Phone_diary_project1;allowuservariables=True;persistsecurityinfo=True";
con.Open();
using (SqlCommand com = con.CreateCommand())
{
com.CommandText = "insert into diary(First," +
"Last," +
"Mobile," +
"Email," +
"" +
"Category) " +
"values(" +
"@prm1," +
"@prm2," +
"@prm3," +
"@prm4," +
"@prm5) ";
com.Parameters.Add("@prm1", SqlDbType.VarChar, 50).Value = textBox1.Text;
com.Parameters.Add("@prm2", SqlDbType.VarChar, 50).Value = textBox2.Text;
com.Parameters.Add("@prm3", SqlDbType.Int).Value = textBox3.Text;
com.Parameters.Add("@prm4", SqlDbType.VarChar, 50).Value = textBox4.Text;
com.Parameters.Add("@prm5", SqlDbType.VarChar, 50).Value = comboBox1.Text;
com.ExecuteNonQuery();
}
con.Close();
}

最佳答案

AllowUserVariables是 MySQL 连接字符串选项:https://mysqlconnector.net/connection-options/#AllowUserVariables . (您还标记了这个问题 mysql 。)
但是,您的代码正在使用 SqlConnection ,这仅用于连接到 Microsoft SQL Server。安装 MySqlConnector从 NuGet 并更改您的代码以使用 MySqlConnection , MySqlCommand , MySqlDbType , 等等。
顺便说一句,你实际上并不需要 AllowUserVariables对于此代码示例; MySQL 语句中始终允许使用命令参数。

关于c# - 解决连接字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64905601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com