标签: sqlconnection

使用C#的'using'语句和自定义对象的函数,我是否需要实现IDisposable?

我有一个像这样的sqlConnection管理器类:

public class SQLConn {
  public string connStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

  private SqlConnection sqlConn;

  public SqlConnection Connection()
  {
      sqlConn = new SqlConnection(connStr);

      return sqlConn;
  }

  public void Open()
  {
        sqlConn .Open();
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我使用带有'using'语句的函数,例如:

var conn = new SQLConn();

using (conn.Connection()) 
{ 
    String query = "Select * from table";
    objSql = new SqlCommand(query, conn.Connection());      

    conn.Open(); 
    DoSomething(); 
}
Run Code Online (Sandbox Code Playgroud)

conn.Connection()返回SqlConnection对象后,using语句是否自动处理连接?或者,我是否必须在SqlConn类上实现IDisposable和自定义Dispose方法?

这甚至是一个好方法吗?我正在使用遗留代码,但我还无法使用ORM,但有没有办法简化现有模式来管理/创建SQL连接?

c# idisposable sqlconnection

8
推荐指数
2
解决办法
2万
查看次数

让SQL连接保持打开有什么缺点?

这似乎是一个简单的问题,但我想知道不调用"close()"函数的缺点.

c# sql-server sqlconnection sqlconnection.close

8
推荐指数
2
解决办法
1972
查看次数

测试sql连接而不抛出异常

要测试我是否可以连接到我的数据库,我执行以下代码:

using (SqlConnection connection = new SqlConnection(myConnectionString))
{
   try
   {
      connection.Open();
      canConnect = true;
   }
   catch (SqlException) { }
}
Run Code Online (Sandbox Code Playgroud)

除非在连接失败时抛出异常,否则此方法有效.有没有其他方法来测试不抛出异常的Sql连接?

编辑:为了增加精度,我问是否有一个简单的方法可以做到这一点,而无需打开连接并捕获可能发生的异常

c# sqlconnection exception

8
推荐指数
1
解决办法
3万
查看次数

SqlDataAdapter在Fill()函数后关闭SqlConnection吗?

是否SqlDataAdapter关闭SqlConnectionFill()功能还是需要关闭它自己?

string cnStr = @"Data Source=TEST;Initial Catalog=Suite;Persist Security Info=True;User ID=app;Password=Immmmmm";
cn = new SqlConnection(cnStr);
SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Date", cn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
adapter.Fill(ds);

cn.Close() // ????????

Console.WriteLine(ds.Tables[0].Rows.Count);
Console.WriteLine(cn.State);
Run Code Online (Sandbox Code Playgroud)

.net c# sql-server sqlconnection sqlconnection.close

8
推荐指数
2
解决办法
2万
查看次数

我在sqlConnection中发现了那些奇怪的属性是什么?

在调查一个非常枯燥乏味的安全漏洞的2005项目时,我调试了一个连接挂起.
检查对象,找到服务器名称,我遇到了这三个小属性:

  • IsShiloh
  • IsKatmaiOrNewer
  • IsYukonOrNewer

那些成员是什么?他们为什么如此出色地命名?

.net c# sqlconnection

8
推荐指数
1
解决办法
438
查看次数

ASP.NET使用SqlConnection连接MySQL

这是保存在web.config以下位置的连接字符串:

<appSettings>
    <add key="conn" value="Driver={MySQL ODBC 5.1 Driver};server=127.0.0.1;uid=root;pwd=1234;database=gis_server;option=3"/>
    </appSettings>
Run Code Online (Sandbox Code Playgroud)

这是连接数据库的代码:

protected bool CheckPasswordBySqlServer(string strEmail, string strPsw)
{
    if (strEmail.ToLower() == "admin")
    {
        return false;
    }
    string str = "select id,Rank,RankEnc,ParentUser,Company from tbl_User where userName=@UserName and password1=@password";
    private string strConn = ConfigurationManager.AppSettings["conn"].ToString();
    SqlConnection sqlConnection = new SqlConnection(strConn);
    bool flag = false;
    try
    {
        try
        {
            sqlConnection.Open();
            SqlCommand sqlCommand = new SqlCommand(str, sqlConnection);
            sqlCommand.Parameters.AddWithValue("UserName", strEmail);
            sqlCommand.Parameters.AddWithValue("Password", strPsw);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            if (!sqlDataReader.Read())
            {
                flag = false;
            }
            else
            { …
Run Code Online (Sandbox Code Playgroud)

.net c# mysql asp.net sqlconnection

8
推荐指数
1
解决办法
2万
查看次数

SqlConnection是否并行处理查询?

如果我打开一个SqlConnectionSQL Server,然后从多个后台线程发出多个查询,所有使用那个连接 - 这些查询是否会按顺序执行(不关心顺序)?

具体来说,如果在一个查询的开头我更改了隔离级别,然后在该查询结束时将其还原 - 这个隔离级别是否有可能适用于其他查询?

我想不是,但想确认一下.

SQL Server 2008 R2

而我在谈论 System.Data.SqlClient.SqlConnection

sql sql-server sqlconnection sql-server-2008-r2

8
推荐指数
1
解决办法
381
查看次数

ASP.NET MVC使用Dapper管理SQLConnection

我正在使用Stack Overflow/Sam Saffron发布的新Dapper Micro ORM快速使用MVC.我想知道在我的控制器中管理SQLConnection对象的最简单方法是什么?我正在做这样简单的事情只是为了旋转一些数据并测试Dapper,但是这样想打开/关闭连接是什么意思?

public class HomeController : Controller
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ajh"].ConnectionString);

    public HomeController()
    {
    }

    public ActionResult Index()
    {
        // get me all comments
        conn.Open();
        var comments = conn.ExecuteMapperQuery<Comment>("select * from Comment");
        conn.Close();

        return View(comments);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc orm sqlconnection

7
推荐指数
1
解决办法
4953
查看次数

调用new SqlConnection()挂起程序

这个让我难过.我甚至没有尝试连接数据库.当这段代码到达我实例化一个新的SqlConnection对象的行时,它就会挂起,而不是抛出异常或任何东西.我试过为2.0编译它.3.5和4.0,他们都挂了.当然它也适用于我的机器和你的机器.但我正在尝试在Windows Server 2008 x64服​​务器上运行此代码,它不会让步.

// example.cs
using System;
using System.Data;
using System.Data.SqlClient;

public class MainClass {
    public static void Main(string[] args) {
        Console.WriteLine("start");
        SqlConnection conn = new SqlConnection(); // hangs here
        Console.WriteLine("finish");              // never makes it here.
    }
}
Run Code Online (Sandbox Code Playgroud)

编译(2.0):c:\ Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe example.cs

.net c# sqlconnection .net-2.0

7
推荐指数
2
解决办法
2779
查看次数

您应该重用SqlConnection,SqlDataAdapter和SqlCommand对象吗?

我正在使用以类似于以下代码的布局编写的DAL对象.我简化了很多代码只是为了显示设置.

public class UserDatabase : IDisposable
{
    private SqlDataAdapter UserDbAdapter;
    private SqlCommand UserSelectCommand;
    private SqlCommand UserInsertCommand;
    private SqlCommand UserUpdateCommand;
    private SqlCommand UserDeleteCommand;

    private System.Data.SqlClient.SqlConnection SQLConnection; 

    public UserDatabase()
    {
        this.SQLConnection = new System.Data.SqlClient.SqlConnection(ConnectionString);
        this.UserDbAdapter= new SqlDataAdapter(); 
        this.UserDbAdapter.DeleteCommand = this.UserDeleteCommand;
        this.UserDbAdapter.InsertCommand = this.UserInsertCommand;
        this.UserDbAdapter.SelectCommand = this.UserSelectCommand;
        this.UserDbAdapter.UpdateCommand = this.UserUpdateCommand;
    }

    private bool FillUsers(DataSet UserDataSet, out int numberOfRecords)
    {
        bool success = true;

        numberOfRecords = 0;
        string errorMsg = null;

        this.UserDbAdapter.SelectCommand = this.GetUsersSelectCommand();

        numberOfRecords = UserDbAdapter.Fill(UserDataSet, UsersTableName);

        return success;
    }

    private SqlCommand GetUserSelectCommand() …
Run Code Online (Sandbox Code Playgroud)

c# sqlcommand sqlconnection sqldataadapter

7
推荐指数
1
解决办法
5926
查看次数