日期:2014-05-18  浏览次数:20337 次

conn.cs 数据库连接文件请老师们帮看一下
using System;
using System.Data;
using System.Data.SqlClient;

namespace Manager
{
/// <summary>
/// Conn 的摘要说明。
/// </summary>
public class Conn
{

private static string strConn = "Server=*.*.*.*;DataBase=**;uid=**;pwd=**;TimeOut=800;";
private SqlConnection conn = new SqlConnection(strConn);

public SqlConnection getInstance()
{
return conn;
}
public void CloseDB()
{
Connection.Close();
} }
}


红色部分是我加的需要修改的地方,请问怎么修改。加上红色部分是不是会能减轻数据库的负担

------解决方案--------------------
就这点代码,
什么都看不出来,
谈不上有什么优化。

没有OPEN,
又何必CLOSE呢?

------解决方案--------------------
public void CloseDB()
{
if(conn.State==ConnectionState.Open)
{
conn.Close();
}
}
这样吧应该
------解决方案--------------------
public void CloseDB()
{
if(conn.State==ConnectionState.Open)
{
conn.Close();
}
}
 YES
------解决方案--------------------
年轻人啊,该好好进步进步了
------解决方案--------------------
前面有open后面对应close比较好,不然没什么用吧
------解决方案--------------------
C# code

            DataTable Table = new DataTable();//实例化表格,用于装载数据
            using(SqlConnection Connection=new SqlConnection(ConnectionString))//实例化sqlConection
            {
                using(SqlCommand Command=new SqlCommand(sql,Connection))
                {
                    Command.CommandType = commandType;//设置查询语句类型
                    if(Parameter!=null)
                    {
                        foreach(SqlParameter Par in Parameter)//如果传进参数就添加参数
                        {
                            Command.Parameters.Add(Par);
                        }
                    }
                    SqlDataAdapter Adapter = new SqlDataAdapter(Command);//借助Adapter做传值给Table
                    Adapter.Fill(Table);//填充Table
                }
            }
            return Table;//返回数据集

------解决方案--------------------
网上下个ms 的sqlhelper研究研究哈