com.Connection = con;它显示错误

时间:2016-03-18 22:18:21

标签: c#

//显示连接字符串con.connection错误.it不被接受

string con =(string) ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.Database1ConnectionString"].ConnectionString;
//string con = Properties.Settings.Default.Database1ConnectionString;
string sql = "SELECT category.Id, category.category, book.bookid FROM category INNER JOIN  book ON category.Id ="+m;
SqlCommand com = new SqlCommand();
com.CommandText = sql;
com.Connection = con;

con.Open()
{
    com.EndExecuteNonQuery()
    con.close;
}

1 个答案:

答案 0 :(得分:4)

Connection属性为SqlConnection,而变量con包含string

以下是如何使其发挥作用:

using (var connection = new SqlConnection(con))
{
    connection.Open();
    using (var command = new SqlCommand(sql, connection))
    {
        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                // your logic to process the response
            }
        }
    }
}