在SQLConnection.close()使用语句中语句、SQLConnection、close

2023-09-03 04:58:41 作者:来一碗小仙女

所有的答案都是有用的。感谢所有谁把精力回答我的问题。干杯:)

我用这个code:

 公共无效InsertMember(会员会员)
    {
        字符串INSERT =INSERT INTO成员(姓名,EntryDate)VALUES(@Name,@Surname,@EntryDate);

        使用(SqlConnection的=新的SqlConnection(sqlConnectionString_WORK))
        {
            在SQLConnection.open();

            使用(的SqlCommand的SqlCommand =新的SqlCommand(INSERT,SqlConnection的))
            {
                sqlCommand.Parameters.Add(@名称,SqlDbType.VarChar)。价值= member.Name;
                sqlCommand.Parameters.Add(@姓,SqlDbType.VarChar)。价值= member.Surname;
                sqlCommand.Parameters.Add(@ EntryDate,SqlDbType.Date)。价值= member.EntryDate;

                sqlCommand.ExecuteNonQuery();
            }
        }
    }
 

它是错的,如果我不加在SQLConnection.close();处理之前,?我的意思是......它没有显示任何错误,没有任何问题......难道不如先关闭它?如果是,为什么?

解决方案

没有必要关闭或出售使用块会照顾你们。

  

下面的示例创建一个SqlConnection,打开它,显示一些   其性质。的连接在结束时自动关闭   using块。

 私有静态无效OpenSqlConnection(字符串的connectionString){
    使用(SqlConnection的连接=新的SqlConnection(的connectionString))
    {
        connection.Open();
        Console.WriteLine(ServerVersion:{0},connection.ServerVersion);
        Console.WriteLine(状态:{0},connection.State);
    }}
 
怎么在 script 标签里直接打开一个页面 用js语句 显示错误 应该怎么写

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close%28v=vs.100%29.aspx

All of your answers were useful. Thank to all who put effort on answering my question. Cheers :)

I'm using this code:

    public void InsertMember(Member member)
    {
        string INSERT = "INSERT INTO Members (Name, Surname, EntryDate) VALUES (@Name, @Surname, @EntryDate)";

        using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))
        {
            sqlConnection.Open();

            using (SqlCommand sqlCommand = new SqlCommand(INSERT, sqlConnection))
            {
                sqlCommand.Parameters.Add("@Name", SqlDbType.VarChar).Value = member.Name;
                sqlCommand.Parameters.Add("@Surname", SqlDbType.VarChar).Value = member.Surname;
                sqlCommand.Parameters.Add("@EntryDate", SqlDbType.Date).Value = member.EntryDate;

                sqlCommand.ExecuteNonQuery();
            }
        }
    }

Is it wrong if I don't add sqlConnection.Close(); before disposing it? I mean... It's not showing any errors, no problems at all... Is it better to Close it first? If yes, why?

解决方案

No need to Close or Dispose the using block will take care of that for you.

The following example creates a SqlConnection, opens it, displays some of its properties. The connection is automatically closed at the end of the using block.

private static void OpenSqlConnection(string connectionString) {
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("State: {0}", connection.State);
    } }

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close%28v=vs.100%29.aspx

 
精彩推荐
图片推荐