Пример #1
0
        public static System.Data.Common.DbConnection GetDbConnection(Model.CodeMaker.DbLinkInfo info)
        {
            System.Data.Common.DbConnection conn = null;
            try
            {
                DataBaseTypeEnum fc           = DataBaseType.GetDataBaseType(info.DbType);
                string           characterSet = ConfigurationManager.AppSettings["CharacterSet"] == null ? "" : ConfigurationManager.AppSettings["CharacterSet"];
                switch (fc)
                {
                case DataBaseTypeEnum.SQLServer:
                    conn = new System.Data.SqlClient.SqlConnection();
                    string port = "";
                    if (!string.IsNullOrEmpty(info.Port))
                    {
                        port = "," + info.Port;
                    }
                    conn.ConnectionString = "Data Source=" + info.DataSource + port + ";Initial Catalog=" + info.DbName + ";User ID=" + info.UserName + ";Password="******"(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST =" + info.DataSource + ")(PORT =" + info.Port + "))) (CONNECT_DATA = (SERVICE_NAME =" + info.DbName + ")))";
                    conn.ConnectionString = "Data Source=" + dataSource + ";User ID=" + info.UserName + ";Password="******"";
                    break;

                case DataBaseTypeEnum.Access:
                    conn = new System.Data.OleDb.OleDbConnection();
                    string AccessDataSource = info.DataSource;
                    if (AccessDataSource.IndexOf("CodeMaker.mdb") > 0)
                    {
                        AccessDataSource = DBConfig.GetConString("AccessCodeMaker");
                    }
                    conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AccessDataSource;
                    break;

                case DataBaseTypeEnum.SQLite:
                    conn = new System.Data.SQLite.SQLiteConnection();
                    string connstring = @"Data Source=" + info.DataSource + ";Version=3;";
                    if (info.PassWord.Trim() != "")
                    {
                        connstring = connstring + "Password="******"";
                    //if (!string.IsNullOrEmpty(characterSet))
                    //    charset = ";Charset=" + characterSet;

                    conn = new MySql.Data.MySqlClient.MySqlConnection();
                    conn.ConnectionString = "server=" + info.DataSource + ";database=" + info.DbName + ";port=" + info.Port + ";user id=" + info.UserName + ";password="******";Allow Zero Datetime=true" + charset;
                    //conn.ConnectionString = "Data Source=" + info.DataSource + ";port=" + info.Port+";Initial Catalog=" + info.DbName + ";User ID=" + info.UserName + ";Password="******"server=" + info.DataSource + ";database=" + info.DbName + ";port=" + info.Port + ";user id=" + info.UserName + ";password="******";Allow Zero Datetime=true";
                    //    break;
                }
                return(conn);
            }
            catch (Exception ex)
            {
                //rstmsg = "操作失败。" + ex.Message;
            }

            return(conn);
        }
Пример #2
0
        public DataSet Run(DbLinkInfo info, string sql, out string sqlmsg, out string rstmsg)
        {
            sqlmsg = "";
            rstmsg = "";
            DataSet ds = null;

            try
            {
                DataBaseTypeEnum fc = DataBaseType.GetDataBaseType(info.DbType);
                System.Data.Common.DbConnection conn = DBConfig.GetDbConnection(info);
                DbHelper.SqlErrorList = null;
                switch (fc)
                {
                case DataBaseTypeEnum.SQLServer:
                    sql = sql + " print '('+convert(varchar(10), @@rowcount)+'行受影响)'";
                    ds  = DbHelper.ExecuteDataSetCurrency(conn, CommandType.Text, sql, null);
                    break;

                case DataBaseTypeEnum.Oracle:
                    //sql = @"declare n number; begin "+sql+" n:=sql%rowcount; dbms_output.put_line(n); end; ";
                    ds = DbHelper.ExecuteDataSetCurrency(conn, CommandType.Text, sql, null);
                    break;

                case DataBaseTypeEnum.Access:
                    ds = DbHelper.ExecuteDataSetCurrency(conn, CommandType.Text, sql, null);
                    break;

                case DataBaseTypeEnum.SQLite:
                    ds = DbHelper.ExecuteDataSetCurrency(conn, CommandType.Text, sql, null);
                    break;

                case DataBaseTypeEnum.MySql:
                    ds = DbHelper.ExecuteDataSetCurrency(conn, CommandType.Text, sql, null);
                    break;
                }

                StringBuilder ser = new StringBuilder();
                switch (fc)
                {
                case DataBaseTypeEnum.SQLServer:

                    if (DbHelper.SqlErrorList != null)
                    {
                        for (int i = 0; i < DbHelper.SqlErrorList.Count; i++)
                        {
                            System.Data.SqlClient.SqlError r = DbHelper.SqlErrorList[i];
                            //ser.Append("从 SQL Server 中获取一个数值错误代码,它表示错误、警告或“未找到数据”消息。" + r.State.ToString());
                            //ser.Append("获取生成错误的提供程序的名称。" + r.Source.ToString());
                            //ser.Append("获取生成错误的 SQL Server 实例的名称。" + r.Server.ToString());
                            //ser.Append("获取生成错误的存储过程或远程过程调用 (RPC) 的名称。" + r.Procedure.ToString());
                            //ser.Append("获取一个标识错误类型的数字。" + r.Number.ToString());
                            //ser.Append("获取对错误进行描述的文本。" + r.Message.ToString());
                            //ser.Append("从包含错误的 Transact-SQL 批命令或存储过程中获取行号。" + r.LineNumber.ToString());
                            //ser.Append("获取从 SQL Server 返回的错误的严重程度。" + r.Class.ToString());
                            if (i > 0)
                            {
                                ser.Append("\r\n");
                            }
                            ser.Append(r.Message);
                        }
                    }
                    break;

                case DataBaseTypeEnum.Oracle:
                    if (DbHelper.OracleErrorList != null)
                    {
                        for (int i = 0; i < DbHelper.OracleErrorList.Count; i++)
                        {
                            Devart.Data.Oracle.OracleError r = DbHelper.OracleErrorList[i];
                            if (i > 0)
                            {
                                ser.Append("\r\n");
                            }
                            ser.Append(r.Message);
                        }
                    }
                    break;

                case DataBaseTypeEnum.Access:
                    if (DbHelper.OleDbErrorList != null)
                    {
                        for (int i = 0; i < DbHelper.OleDbErrorList.Count; i++)
                        {
                            System.Data.OleDb.OleDbError r = DbHelper.OleDbErrorList[i];
                            if (i > 0)
                            {
                                ser.Append("\r\n");
                            }
                            ser.Append(r.Message);
                        }
                    }
                    break;
                }
                sqlmsg = ser.ToString();
            }
            catch (Exception ex)
            {
                rstmsg = "操作失败。" + ex.Message;
            }
            return(ds);
        }