| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1990 人关注过本帖
标题:怎么解决烦琐的数据库连接^^^^^^^
只看楼主 加入收藏
jilonghao
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-12-1
结帖率:0
收藏
 问题点数:0 回复次数:9 
怎么解决烦琐的数据库连接^^^^^^^
最近,我遇到一个非常苦恼的问题:在C#中,许多窗体都需要使用到数据库中的内容,可是每个窗体中都得输入连接字符串,连接对象,再打开……
有没有更好的方法,能只连接一次,哪个需要就调用的,请大家帮帮忙????
搜索更多相关主题的帖子: 数据库 
2008-12-03 20:16
yep
Rank: 1
来 自:河北
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-7
收藏
得分:0 
分层来写啊 ,在连接数据库层写相关的代码
2008-12-03 21:46
wolf44
Rank: 1
来 自:河北
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-2-11
收藏
得分:0 
网上有现成的sqlhelper 直接用就可以了

2008-12-03 21:54
小红
Rank: 1
来 自:universit
等 级:新手上路
威 望:1
帖 子:15
专家分:0
注 册:2008-11-18
收藏
得分:0 
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Security.Principal;

/// <summary>
/// Summary description for Class1
/// </summary>
namespace DataBase
{
    public class DBCom
    {
        //取得一个数据库的连接
        public  static SqlConnection GetConnection()
        {
            
            SqlConnection sqlconn = new SqlConnection();
            try
            {
               
                string strCon = "server=192.168.1.88;database=xixi;uid=xiaoxiao;pwd=1234";
               
                sqlconn.ConnectionString = strCon;
            
                return sqlconn;

            }
            catch (SqlException e)
            {
                System.Console.WriteLine(e.Message.ToString());
                return null;
            }
        }

        //========================================================================

        //执行sql语句返回所影响的行数
        public int ExecuteSql(string sql)
        {
           
            SqlConnection conn = DBCom.GetConnection();
            SqlCommand cmd = conn.CreateCommand();
             = sql.Trim();
            try
            {
                conn.Open();
                int count = cmd.ExecuteNonQuery();
                return count;
            }
            catch (SqlException ex)
            {
                System.Console.WriteLine(ex.Message.ToString());
                return 0;
            }
        }

        //执行存储过程
        public  int ExecuteProc(string procName, SqlParameter[] sp)
        {
            SqlConnection conn = DBCom.GetConnection();
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
             = procName.Trim();
             = CommandType.StoredProcedure;

            foreach (SqlParameter s in sp)
            {
                cmd.Parameters.Add(s);
            }

            try
            {
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                return i;
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message.ToString());
                return 0;
            }
        }

        //==============================================================================

        //根据sql 返回数据
        public  SqlDataReader GetDataReader(string sql)
        {
            SqlConnection conn = DBCom.GetConnection();
            SqlCommand cmd = conn.CreateCommand();
             = sql.Trim();
            try
            {
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader(System.);
                return dr;
            }
            catch (SqlException ex)
            {
                System.Console.WriteLine(ex.Message.ToString());
                return null;
            }
        }

        //===========================================================================

        public  DataSet GetDataSet(string Sql, string TableName)
        {
            SqlConnection conn = DBCom.GetConnection();
            try
            {
                conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(Sql, conn);
                DataSet ds = new DataSet();
                adapter.Fill(ds, TableName);
                conn.Close();
                return ds;
            }
            catch (SqlException ex)
            {
                System.Console.WriteLine(ex.Message.ToString());
                return null;
            }
        }

        //通过存储过程返回 数据集
        public  DataSet GetDataSet1(string procname, SqlParameter[] sp, string tableName)
        {
            SqlConnection conn = DBCom.GetConnection();
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();

             = CommandType.StoredProcedure;
             = procname.Trim();

            try
            {
                conn.Open();
                cmd.Connection = conn;
                foreach (SqlParameter p in sp)
                {
                    if (p == null)
                    {
                        break;
                    }
                    else
                    {
                        cmd.Parameters.Add(p);
                    }

                }

                //cmd.ExecuteNonQuery();
                adapter.SelectCommand = cmd;
                adapter.Fill(ds, tableName);
                conn.Close();
                return ds;
            }
            catch (SqlException ex)
            {
                System.Console.WriteLine(ex.Message.ToString());
                return null;
            }
        }
        public string re(string a)
        {
            string s = a.Replace("-_COMPART_-", "");
            while (s.Length > 0)
            {
                if (s.LastIndexOf("\r") == s.Length - 1 || s.LastIndexOf("\n") == s.Length - 1)
                {
                    s = s.Remove(s.Length - 1, 1);
                }
                else
                {
                    break;
                }
            }
            while (s.Length > 0)
            {
                if (s.IndexOf("\r") == 0 || s.IndexOf("\n") == 0)
                {
                    s = s.Remove(0, 1);
                }
                else
                {
                    return s;
                }
            }
            return s;
        }
        public int ExecuteProc1(string procName, SqlParameter[] sp)
        {

            SqlConnection conn = DBCom.GetConnection();
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();

             = procName.Trim();

             = CommandType.StoredProcedure;



            foreach (SqlParameter s in sp)
            {

                try
                {

                    cmd.Parameters.Add(s);

                }

                catch (Exception ex)
                {

                    System.Console.WriteLine(ex.Message.ToString());

                    return 0;

                }

            }



            try
            {

                int i = cmd.ExecuteNonQuery();

                cmd.Parameters.Clear();

                conn.Close();

                return i;

            }

            catch (System.Exception ex)
            {

                System.Console.WriteLine(ex.Message.ToString());

                return 0;

            }

        }



    }
}
2008-12-03 22:57
hebingbing
Rank: 6Rank: 6
来 自:黄土高坡
等 级:贵宾
威 望:27
帖 子:3417
专家分:371
注 册:2007-10-22
收藏
得分:0 
sqlhelp
微软的……
2008-12-04 08:44
jilonghao
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-12-1
收藏
得分:0 
hehe
谢谢了,的确可行!!!!!!!!!!
2008-12-05 15:55
zuglog2133
Rank: 1
等 级:新手上路
帖 子:26
专家分:5
注 册:2008-10-10
收藏
得分:0 
还可以在配置文件中写
2008-12-05 18:10
zhjesse
Rank: 1
来 自:西藏拉萨
等 级:新手上路
帖 子:205
专家分:0
注 册:2007-4-3
收藏
得分:0 
分层

比他写个数据库操作类

然后需要的地方调用就可以了

想了解西藏的天葬文化、藏北比如骷髅墙吗?[url=http://www.]【雪域藏北论坛】[/url]给你诠释!
2008-12-07 10:56
守望♂幸福
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2006-4-25
收藏
得分:0 
有遇到楼上的姐姐

偶是偶滴偶像!!~ QQ:375490156
2008-12-09 20:36
zw83120030
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-10-27
收藏
得分:0 
回复 楼主 jilonghao 的帖子
数据库连接不算烦琐吧
2008-12-10 09:43
快速回复:怎么解决烦琐的数据库连接^^^^^^^
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036637 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved