我正在尝试连接到我为Xamarin iOS应用设置的SQL服务器,我遵循了一些指南,建议使用system.data.sqlclient。 当我运行代码时,open方法会弹出this error
这是我编写的用于连接数据库的类(敏感数据已被替换):
class clsDBConnector
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
SqlConnection connection = new SqlConnection();
public clsDBConnector()
{
builder.DataSource = "my_server";
builder.UserID = "my_userid";
builder.Password = "my_password";
builder.InitialCatalog = "my_db";
connection.ConnectionString = builder.ConnectionString;
}
public void Connect()
{
connection.Open();
}
public void Close()
{
connection.Close();
}
public SqlDataReader Query(string sql)
{
SqlCommand command = new SqlCommand(sql, connection);
return command.ExecuteReader();
}
}
这是我在视图控制器中创建的用于使用它的功能:
public void displayData(ref List<string> test)
{
clsDBConnector dbConnector = new clsDBConnector();
SqlDataReader dr;
string sqlStr;
dbConnector.Connect();
sqlStr = "SELECT testName FROM dbo.tblTest";
dr = dbConnector.Query(sqlStr);
while (dr.Read())
{
test.Add(dr[0].ToString());
}
{