代码如下:
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Writing_Data_To_SQL_Database_From_V_S
{
class Program
{
static void Main(string[] args)
{
//1). Declare connection string
string connectionString = "Data Source = LAPTOP - C5FEB2D0\\SQLEXPRESS;Initial Catalog = Upskilling;Integrated Security = True;MultipleActiveResultsSets = True;";
//2). Write to Student table
// Make use of the PopulateStudentTable method declared in Line 29 & "connectionString" as the argument
PopulateStudentTable(connectionString);
}
//3). This is outside the main method.
// Declare specific method and argument to be used in populating said table (Student table)
static void PopulateStudentTable(string connectionString)
{
//3a). Insert 1 record into the Student table
string query = @"Insert Into [dbo].[Student](FirstName, LastName, City)
Values('Dan, 'Smith', 'London')";
//3b). Now, Connect to the SQL Server database
//3c). Use the Dependency - "Using System.Data.SqlClient;"
using (SqlConnection connection = new SqlConnection(connectionString))
{
//3d). Reference the "connection" decalred in Line 34 - for it to open the database.
// Use the Open()
connection.Open();
//3e). Execute the command using a method that takes 2 parameters - "query" and "connection string"
// "query" & "connection" string were declared in Line 29 & 34 respectively.
using(SqlCommand command = new SqlCommand(query, connection))
{
//3f). Use the command - "command" & the method - "ExecuteNonQuery()"
command.ExecuteNonQuery();
}
}
}
}
}
我收到的错误消息是:
未处理的异常:System.ArgumentException:不支持关键字: “ multipleactiveresultssets”。在 System.Data.Common.DbConnectionOptions.ParseInternal(哈希表 可解析的,字符串连接字符串,布尔值buildChain,哈希表 同义词,布尔值firstKey)在 System.Data.Common.DbConnectionOptions..ctor(字符串connectionString, 哈希表同义词,布尔值useOdbcRules)位于 System.Data.SqlClient.SqlConnectionString..ctor(字符串 connectionString)在 System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(字符串 connectionString,DbConnectionOptions先前)在 System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey 键,DbConnectionPoolGroupOptions poolOptions,DbConnectionOptions& userConnectionOptions) System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey 键) System.Data.SqlClient.SqlConnection.set_ConnectionString(字符串值) 在System.Data.SqlClient.SqlConnection..ctor(String connectionString, SqlCredential凭据)位于 System.Data.SqlClient.SqlConnection..ctor(String connectionString)
在 Writing_Data_To_SQL_Database_From_V_S.Program.PopulateStudentTable(String connectionString)在 C:\ Users \ opeye \ Documents \ TESTING \ UPSKILLING \ UPSKILLING PROJECTS \ Writing_Data_To_SQL_Database_From_V_S \ Writing_Data_To_SQL_Database_From_V_S \ Program.cs:line 37在Writing_Data_To_SQL_Database_From_V_S.Program.Main(String [] args)在C:\ Users \ opeye \ Documents \ TESTING \ UPSKILLING \ UPSKILLING PROJECTS \ Writing_Data_To_SQL_Database_From_V_S \ Writing_Data_To_SQL_Database_From_V_S \ Program.cs:line 23按任意键继续。 。
到目前为止,我还没有找到解决方案。
感谢您的帮助。
答案 0 :(得分:1)
您输错了“ MultipleActiveResultsSets”。其中的“结果”不是复数。
正确的方法:“ MultipleActiveResultSets”。