在SQLConnection.close()使用语句中 [英] SqlConnection.Close() inside using statement

查看:336
本文介绍了在SQLConnection.close()使用语句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我用这个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);
    }}
 

<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close%28v=vs.100%29.aspx">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

这篇关于在SQLConnection.close()使用语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆