static void Main(string[] args)
    {
        ADODB.Connection conn = null;
        ADODB.Recordset  rs   = null;



        try
        {
            ////////////////////////////////////////////////////////////////////////////////
            // Connect to the data source.
            //

            Console.WriteLine("Connecting to the database ...");

            // Get the connection string
            string connStr = string.Format("Provider=SQLOLEDB;Data Source={0};Initial Catalog={1};Integrated Security=SSPI",
                                           ".\\sqlexpress", "SQLServer2005DB");

            // Open the connection
            conn = new ADODB.Connection();
            conn.Open(connStr, null, null, 0);


            ////////////////////////////////////////////////////////////////////////////////
            // Build and Execute an ADO Command.
            // It can be a SQL statement (SELECT/UPDATE/INSERT/DELETE), or a stored
            // procedure call. Here is the sample of an INSERT command.
            //

            Console.WriteLine("Inserting a record to the CountryRegion table...");

            // 1. Create a command object
            ADODB.Command cmdInsert = new ADODB.Command();

            // 2. Assign the connection to the command
            cmdInsert.ActiveConnection = conn;

            // 3. Set the command text
            // SQL statement or the name of the stored procedure
            cmdInsert.CommandText = "INSERT INTO CountryRegion(CountryRegionCode, Name, ModifiedDate)"
                                    + " VALUES (?, ?, ?)";

            // 4. Set the command type
            // ADODB.CommandTypeEnum.adCmdText for oridinary SQL statements;
            // ADODB.CommandTypeEnum.adCmdStoredProc for stored procedures.
            cmdInsert.CommandType = ADODB.CommandTypeEnum.adCmdText;

            // 5. Append the parameters

            // Append the parameter for CountryRegionCode (nvarchar(20)
            ADODB.Parameter paramCode = cmdInsert.CreateParameter(
                "CountryRegionCode",                        // Parameter name
                ADODB.DataTypeEnum.adVarChar,               // Parameter type (nvarchar(20))
                ADODB.ParameterDirectionEnum.adParamInput,  // Parameter direction
                20,                                         // Max size of value in bytes
                "ZZ" + DateTime.Now.Millisecond);           // Parameter value
            cmdInsert.Parameters.Append(paramCode);

            // Append the parameter for Name (nvarchar(200))
            ADODB.Parameter paramName = cmdInsert.CreateParameter(
                "Name",                                     // Parameter name
                ADODB.DataTypeEnum.adVarChar,               // Parameter type (nvarchar(200))
                ADODB.ParameterDirectionEnum.adParamInput,  // Parameter direction
                200,                                        // Max size of value in bytes
                "Test Region Name");                        // Parameter value
            cmdInsert.Parameters.Append(paramName);

            // Append the parameter for ModifiedDate (datetime)
            ADODB.Parameter paramModifiedDate = cmdInsert.CreateParameter(
                "ModifiedDate",                             // Parameter name
                ADODB.DataTypeEnum.adDate,                  // Parameter type (datetime)
                ADODB.ParameterDirectionEnum.adParamInput,  // Parameter direction
                -1,                                         // Max size (ignored for datetime)
                DateTime.Now);                              // Parameter value
            cmdInsert.Parameters.Append(paramModifiedDate);


            // 6. Execute the command
            object nRecordsAffected = Type.Missing;
            object oParams          = Type.Missing;
            cmdInsert.Execute(out nRecordsAffected, ref oParams,
                              (int)ADODB.ExecuteOptionEnum.adExecuteNoRecords);


            ////////////////////////////////////////////////////////////////////////////////
            // Use the Recordset Object.
            // http://msdn.microsoft.com/en-us/library/ms681510.aspx
            // Recordset represents the entire set of records from a base table or the
            // results of an executed command. At any time, the Recordset object refers to
            // only a single record within the set as the current record.
            //

            Console.WriteLine("Enumerating the records in the CountryRegion table");

            // 1. Create a Recordset object
            rs = new ADODB.Recordset();

            // 2. Open the Recordset object
            string strSelectCmd = "SELECT * FROM CountryRegion"; // WHERE ...
            rs.Open(strSelectCmd,                                // SQL statement / table,view name /
                                                                 // stored procedure call / file name
                    conn,                                        // Connection / connection string
                    ADODB.CursorTypeEnum.adOpenForwardOnly,      // Cursor type. (forward-only cursor)
                    ADODB.LockTypeEnum.adLockOptimistic,         // Lock type. (locking records only
                                                                 // when you call the Update method.
                    (int)ADODB.CommandTypeEnum.adCmdText);       // Evaluate the first parameter as
                                                                 // a SQL command or stored procedure.

            // 3. Enumerate the records by moving the cursor forward

            // Move to the first record in the Recordset
            rs.MoveFirst();
            while (!rs.EOF)
            {
                // When dumping a SQL-Nullable field in the table, need to test it for DBNull.Value.
                string code = (rs.Fields["CountryRegionCode"].Value == DBNull.Value) ?
                              "(DBNull)" : rs.Fields["CountryRegionCode"].Value.ToString();

                string name = (rs.Fields["Name"].Value == DBNull.Value) ?
                              "(DBNull)" : rs.Fields["Name"].Value.ToString();

                DateTime modifiedDate = (rs.Fields["ModifiedDate"].Value == DBNull.Value) ?
                                        DateTime.MinValue : (DateTime)rs.Fields["ModifiedDate"].Value;

                Console.WriteLine(" {2} \t{0}\t{1}", code, name, modifiedDate.ToString("yyyy-MM-dd"));

                // Move to the next record
                rs.MoveNext();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("The application throws the error: {0}", ex.Message);
            if (ex.InnerException != null)
            {
                Console.WriteLine("Description: {0}", ex.InnerException.Message);
            }
        }
        finally
        {
            ////////////////////////////////////////////////////////////////////////////////
            // Clean up objects before exit.
            //

            Console.WriteLine("Closing the connections ...");

            // Close the record set if it is open
            if (rs != null && rs.State == (int)ADODB.ObjectStateEnum.adStateOpen)
            {
                rs.Close();
            }

            // Close the connection to the database if it is open
            if (conn != null && conn.State == (int)ADODB.ObjectStateEnum.adStateOpen)
            {
                conn.Close();
            }
        }
    }
Ejemplo n.º 2
0
    static void Main(string[] args)
    {
        ADODB.Connection conn = null;
        ADODB.Recordset  rs   = null;

        try
        {
            ////////////////////////////////////////////////////////////////////////////////
            // Connect to the data source.
            //

            Console.WriteLine("Connecting to the database ...");

            // Get the connection string from App.config. (The data source is created in the
            // sample SQLServer2005DB)
            string connStr = ConfigurationManager.ConnectionStrings["SQLServer2005DB"].
                             ConnectionString;

            // Open the connection
            conn = new ADODB.Connection();
            conn.Open(connStr, null, null, 0);


            ////////////////////////////////////////////////////////////////////////////////
            // Build and Execute an ADO Command.
            // It can be a SQL statement (SELECT/UPDATE/INSERT/DELETE), or a stored
            // procedure call. Here is the sample of an INSERT command.
            //

            Console.WriteLine("Inserting a record to the Person table...");

            // 1. Create a command object
            ADODB.Command cmdInsert = new ADODB.Command();

            // 2. Assign the connection to the command
            cmdInsert.ActiveConnection = conn;

            // 3. Set the command text
            // SQL statement or the name of the stored procedure
            cmdInsert.CommandText = "INSERT INTO Person(LastName, FirstName, EnrollmentDate, Picture)"
                                    + " VALUES (?, ?, ?, ?)";

            // 4. Set the command type
            // ADODB.CommandTypeEnum.adCmdText for oridinary SQL statements;
            // ADODB.CommandTypeEnum.adCmdStoredProc for stored procedures.
            cmdInsert.CommandType = ADODB.CommandTypeEnum.adCmdText;

            // 5. Append the parameters

            // Append the parameter for LastName (nvarchar(50)
            ADODB.Parameter paramLN = cmdInsert.CreateParameter(
                "LastName",                                 // Parameter name
                ADODB.DataTypeEnum.adVarChar,               // Parameter type (nvarchar(50))
                ADODB.ParameterDirectionEnum.adParamInput,  // Parameter direction
                50,                                         // Max size of value in bytes
                "Zhang");                                   // Parameter value
            cmdInsert.Parameters.Append(paramLN);

            // Append the parameter for FirstName (nvarchar(50))
            ADODB.Parameter paramFN = cmdInsert.CreateParameter(
                "FirstName",                                // Parameter name
                ADODB.DataTypeEnum.adVarChar,               // Parameter type (nvarchar(50))
                ADODB.ParameterDirectionEnum.adParamInput,  // Parameter direction
                50,                                         // Max size of value in bytes
                "Rongchun");                                // Parameter value
            cmdInsert.Parameters.Append(paramFN);

            // Append the parameter for EnrollmentDate (datetime)
            ADODB.Parameter paramED = cmdInsert.CreateParameter(
                "EnrollmentDate",                           // Parameter name
                ADODB.DataTypeEnum.adDate,                  // Parameter type (datetime)
                ADODB.ParameterDirectionEnum.adParamInput,  // Parameter direction
                -1,                                         // Max size (ignored for datetime)
                DateTime.Now);                              // Parameter value
            cmdInsert.Parameters.Append(paramED);

            // Append the parameter for Picture (image)

            // Read the image file into a safe array of bytes
            Byte[]          bImage     = ReadImage(@"MSDN.jpg");
            ADODB.Parameter paramImage = cmdInsert.CreateParameter(
                "Picture",                                  // Parameter name
                ADODB.DataTypeEnum.adLongVarBinary,         // Parameter type (Image)
                ADODB.ParameterDirectionEnum.adParamInput,  // Parameter direction
                bImage != null ? bImage.Length : 1,         // Max size of value in bytes
                bImage);                                    // Parameter value
            cmdInsert.Parameters.Append(paramImage);

            // 6. Execute the command
            object nRecordsAffected = Type.Missing;
            object oParams          = Type.Missing;
            cmdInsert.Execute(out nRecordsAffected, ref oParams,
                              (int)ADODB.ExecuteOptionEnum.adExecuteNoRecords);


            ////////////////////////////////////////////////////////////////////////////////
            // Use the Recordset Object.
            // http://msdn.microsoft.com/en-us/library/ms681510.aspx
            // Recordset represents the entire set of records from a base table or the
            // results of an executed command. At any time, the Recordset object refers to
            // only a single record within the set as the current record.
            //

            Console.WriteLine("Enumerating the records in the Person table");

            // 1. Create a Recordset object
            rs = new ADODB.Recordset();

            // 2. Open the Recordset object
            string strSelectCmd = "SELECT * FROM Person";   // WHERE ...
            rs.Open(strSelectCmd,                           // SQL statement / table,view name /
                                                            // stored procedure call / file name
                    conn,                                   // Connection / connection string
                    ADODB.CursorTypeEnum.adOpenForwardOnly, // Cursor type. (forward-only cursor)
                    ADODB.LockTypeEnum.adLockOptimistic,    // Lock type. (locking records only
                                                            // when you call the Update method.
                    (int)ADODB.CommandTypeEnum.adCmdText);  // Evaluate the first parameter as
                                                            // a SQL command or stored procedure.

            // 3. Enumerate the records by moving the cursor forward
            rs.MoveFirst();  // Move to the first record in the Recordset
            while (!rs.EOF)
            {
                int nPersonId = (int)rs.Fields["PersonID"].Value;

                // When dumping a SQL-Nullable field in the table, need to test it for
                // DBNull.Value.
                string strFirstName = (rs.Fields["FirstName"].Value == DBNull.Value) ?
                                      "(DBNull)" : rs.Fields["FirstName"].Value.ToString();

                string strLastName = (rs.Fields["LastName"].Value == DBNull.Value) ?
                                     "(DBNull)" : rs.Fields["LastName"].Value.ToString();

                Console.WriteLine("{0}\t{1} {2}", nPersonId, strFirstName, strLastName);

                // Update the current record while enumerating the Recordset.
                //rs.Fields["XXXX"].Value = XXXX
                //rs.Update(); [-or-] rs.UpdateBatch(); outside the loop.

                rs.MoveNext();   // Move to the next record
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("The application throws the error: {0}", ex.Message);
            if (ex.InnerException != null)
            {
                Console.WriteLine("Description: {0}", ex.InnerException.Message);
            }
        }
        finally
        {
            ////////////////////////////////////////////////////////////////////////////////
            // Clean up objects before exit.
            //

            Console.WriteLine("Closing the connections ...");

            // Close the record set if it is open
            if (rs != null && rs.State == (int)ADODB.ObjectStateEnum.adStateOpen)
            {
                rs.Close();
            }

            // Close the connection to the database if it is open
            if (conn != null && conn.State == (int)ADODB.ObjectStateEnum.adStateOpen)
            {
                conn.Close();
            }
        }
    }
Ejemplo n.º 3
0
    static void Main(string[] args)
    {
        ADODB.Connection conn = null;
        ADODB.Recordset rs = null;

        try
        {
            ////////////////////////////////////////////////////////////////////////////////
            // 连接数据源.
            //

            Console.WriteLine("正在连接数据库 ...");

            // 获取连接字符串
            string connStr = string.Format("Provider=SQLOLEDB;Data Source={0};Initial Catalog={1};Integrated Security=SSPI",
                ".\\sqlexpress", "SQLServer2005DB");

            // 打开连接
            conn = new ADODB.Connection();
            conn.Open(connStr, null, null, 0);

            ////////////////////////////////////////////////////////////////////////////////
            // 编写并执行ADO命令.
            // 可以是SQL指令(SELECT/UPDATE/INSERT/DELETE),或是调用存储过程.
            // 此处是一个INSERT命令示例.
            //

            Console.WriteLine("将一条记录插入表CountryRegion中...");

            // 1. 生成一个Command对象
            ADODB.Command cmdInsert = new ADODB.Command();

            // 2. 将连接赋值于命令
            cmdInsert.ActiveConnection = conn;

            // 3. 设置命令文本
            //  SQL指令或者存储过程名
            cmdInsert.CommandText = "INSERT INTO CountryRegion(CountryRegionCode, Name, ModifiedDate)"
                + " VALUES (?, ?, ?)";

            // 4. 设置命令类型
            // ADODB.CommandTypeEnum.adCmdText 用于普通的SQL指令;
            // ADODB.CommandTypeEnum.adCmdStoredProc 用于存储过程.
            cmdInsert.CommandType = ADODB.CommandTypeEnum.adCmdText;

            // 5. 添加参数

            //  CountryRegionCode (nvarchar(20)参数的添加
            ADODB.Parameter paramCode = cmdInsert.CreateParameter(
                "CountryRegionCode",                        // 参数名
                ADODB.DataTypeEnum.adVarChar,               // 参数类型 (nvarchar(20))
                ADODB.ParameterDirectionEnum.adParamInput,  // 参数类型
                20,                                         // 参数的最大长度
                "ZZ"+DateTime.Now.Millisecond);             // 参数值
            cmdInsert.Parameters.Append(paramCode);

            // Name (nvarchar(200))参数的添加
            ADODB.Parameter paramName = cmdInsert.CreateParameter(
                "Name",                                     // 参数名
                ADODB.DataTypeEnum.adVarChar,               // 参数类型 (nvarchar(200))
                ADODB.ParameterDirectionEnum.adParamInput,  // 参数传递方向
                200,                                        // 参数的最大长度
                "Test Region Name");                        // 参数值
            cmdInsert.Parameters.Append(paramName);

            // ModifiedDate (datetime)参数的添加
            ADODB.Parameter paramModifiedDate = cmdInsert.CreateParameter(
                "ModifiedDate",                             // 参数名
                ADODB.DataTypeEnum.adDate,                  // 参数类型 (datetime)
                ADODB.ParameterDirectionEnum.adParamInput,  // 参数传递方向
                -1,                                         // 参数的最大长度 (datetime忽视该值)
                DateTime.Now);                              // 参数值
            cmdInsert.Parameters.Append(paramModifiedDate);

            // 6. 执行命令
            object nRecordsAffected = Type.Missing;
            object oParams = Type.Missing;
            cmdInsert.Execute(out nRecordsAffected, ref oParams,
                (int)ADODB.ExecuteOptionEnum.adExecuteNoRecords);

            ////////////////////////////////////////////////////////////////////////////////
            // 使用Recordset对象.
            // http://msdn.microsoft.com/en-us/library/ms681510.aspx
            // Recordset表示了数据表中记录或执行命令获得的结果的集合。
            // 在任何时候, Recordset对象都指向集合中的单条记录,并将
            // 该记录作为它的当前记录。
            //

            Console.WriteLine("列出表CountryRegion中的所有记录");

            // 1. 生成Recordset对象
            rs = new ADODB.Recordset();

            // 2. 打开Recordset对象
            string strSelectCmd = "SELECT * FROM CountryRegion"; // WHERE ...
            rs.Open(strSelectCmd,                                // SQL指令/表,视图名 /
                                                                 // 存储过程调用 /文件名
                conn,                                            // 连接对象/连接字符串
                ADODB.CursorTypeEnum.adOpenForwardOnly,          // 游标类型. (只进游标)
                ADODB.LockTypeEnum.adLockOptimistic,	         // 锁定类型. (仅当需要调用
                                                                 // 更新方法时,才锁定记录)
                (int)ADODB.CommandTypeEnum.adCmdText);	         // 将第一个参数视为SQL命令
                                                                 // 或存储过程.

            // 3. 通过向前移动游标列举记录

            // 移动到Recordset中的第一条记录
            rs.MoveFirst();
            while (!rs.EOF)
            {
                // 当在表中定义了一个可空字段,需要检验字段中的值是否为DBNull.Value.
                string code = (rs.Fields["CountryRegionCode"].Value == DBNull.Value) ?
                    "(DBNull)" : rs.Fields["CountryRegionCode"].Value.ToString();

                string name = (rs.Fields["Name"].Value == DBNull.Value) ?
                    "(DBNull)" : rs.Fields["Name"].Value.ToString();

                DateTime modifiedDate = (rs.Fields["ModifiedDate"].Value == DBNull.Value) ?
                    DateTime.MinValue : (DateTime)rs.Fields["ModifiedDate"].Value;

                Console.WriteLine(" {2} \t{0}\t{1}", code, name, modifiedDate.ToString("yyyy-MM-dd"));

                // 移动到下一条记录
                rs.MoveNext();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("应用程序出现错误: {0}", ex.Message);
            if (ex.InnerException != null)
                Console.WriteLine("描述: {0}", ex.InnerException.Message);
        }
        finally
        {
            ////////////////////////////////////////////////////////////////////////////////
            // 退出前清理对象.
            //

            Console.WriteLine("正在关闭连接 ...");

            // 关闭record set,当它处于打开状态时
            if (rs != null && rs.State == (int)ADODB.ObjectStateEnum.adStateOpen)
                rs.Close();

            // 关闭数据库连接,当它处于打开状态时
            if (conn != null && conn.State == (int)ADODB.ObjectStateEnum.adStateOpen)
                conn.Close();
        }
    }
Ejemplo n.º 4
0
        //式样号信息更新或者委托信息发送。
        private void PRINT_Click(string plsqlPackageName, ArrayList list, string informationText)
        {
            GeneralCommon.GStatusBar.Panels[0].Text = "提示信息:操作未能成功...!!";
            string ret_Result_ErrMsg;
            //int ret_Result_ErrCode = 0;
            string ret_Result_ErrCode;

            ADODB.Command adoCmd;
            try
            {
                if (GeneralCommon.M_CN1.State == 0)
                {
                    if (GeneralCommon.GF_DbConnect() == false)
                    {
                        return;
                    }
                }
                Cursor.Current = Cursors.WaitCursor;
                GeneralCommon.M_CN1.CursorLocation = ADODB.CursorLocationEnum.adUseServer;
                adoCmd = new ADODB.Command();
                adoCmd.ActiveConnection = GeneralCommon.M_CN1;
                adoCmd.CommandType      = ADODB.CommandTypeEnum.adCmdStoredProc;
                adoCmd.CommandText      = plsqlPackageName;
                GeneralCommon.M_CN1.BeginTrans();
                for (int i = 1; i <= list.Count; i++)
                {
                    adoCmd.Parameters.Append(adoCmd.CreateParameter("", ADODB.DataTypeEnum.adVariant, ADODB.ParameterDirectionEnum.adParamInput, 0, null));
                }

                object value = null;
                adoCmd.Parameters.Append(adoCmd.CreateParameter("arg_e_code", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamOutput, 1, value));
                adoCmd.Parameters.Append(adoCmd.CreateParameter("arg_e_msg", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamOutput, 256, value));
                for (int i = 0; i <= list.Count - 1; i++)
                {
                    adoCmd.Parameters[i].Value = list[i].ToString() == "" ? "" : list[i].ToString();
                }

                adoCmd.Execute(out value);
                if (plsqlPackageName == "AQC1061P")
                {
                    if (adoCmd.Parameters["arg_e_code"].Value.ToString() != "YY")
                    {
                        //ret_Result_ErrCode = System.Convert.ToInt32(adoCmd.Parameters["arg_e_code"].Value);
                        ret_Result_ErrCode = (string)(adoCmd.Parameters["arg_e_code"].Value);
                        ret_Result_ErrMsg  = (string)(adoCmd.Parameters["arg_e_msg"].Value);

                        GeneralCommon.sErrMessg = (string)("Error Code : " + ret_Result_ErrCode + "\r\n" + "Error Mesg : " + ret_Result_ErrMsg);
                        GeneralCommon.Gp_MsgBoxDisplay(GeneralCommon.sErrMessg, "W", this.Text);
                        Cursor.Current = Cursors.Default;
                        adoCmd         = null;
                        GeneralCommon.M_CN1.RollbackTrans();
                    }
                    else
                    {
                        GeneralCommon.M_CN1.CommitTrans();
                        GeneralCommon.M_CN1.Close();
                        GeneralCommon.GStatusBar.Panels[0].Text = informationText;
                        adoCmd         = null;
                        Cursor.Current = Cursors.Default;
                    }
                }
                else
                {
                    if (adoCmd.Parameters["arg_e_code"].Value.ToString() != "0")
                    {
                        //ret_Result_ErrCode = System.Convert.ToInt32(adoCmd.Parameters["arg_e_code"].Value);
                        ret_Result_ErrCode = (string)(adoCmd.Parameters["arg_e_code"].Value);
                        ret_Result_ErrMsg  = (string)(adoCmd.Parameters["arg_e_msg"].Value);

                        GeneralCommon.sErrMessg = (string)("Error Code : " + ret_Result_ErrCode + "\r\n" + "Error Mesg : " + ret_Result_ErrMsg);
                        GeneralCommon.Gp_MsgBoxDisplay(GeneralCommon.sErrMessg, "W", this.Text);
                        Cursor.Current = Cursors.Default;
                        adoCmd         = null;
                        GeneralCommon.M_CN1.RollbackTrans();
                    }
                    else
                    {
                        GeneralCommon.M_CN1.CommitTrans();
                        GeneralCommon.M_CN1.Close();
                        GeneralCommon.GStatusBar.Panels[0].Text = informationText;
                        adoCmd         = null;
                        Cursor.Current = Cursors.Default;
                    }
                }
            }
            catch (Exception ex)
            {
                adoCmd = null;
                GeneralCommon.M_CN1.RollbackTrans();
                Cursor.Current = Cursors.Default;
                GeneralCommon.Gp_MsgBoxDisplay((string)("Gf_ExecProcedure Error : " + ex.Message), "W", "警告");
            }
        }
Ejemplo n.º 5
0
        private string Gp_LOC_Exec(string sAddrText, string cbocurText)
        {
            // string sQuery = "{call WGA1040C.P_LOC_TUN ('" + cbocurText + "','" + sAddrText + "',?)}";
            string ret_Result_ErrMsg;
            int    ret_Result_ErrCode = 0;

            ADODB.Command adoCmd;
            try
            {
                if (GeneralCommon.M_CN1.State == 0)
                {
                    if (GeneralCommon.GF_DbConnect() == false)
                    {
                        return("ERROR");
                    }
                }
                Cursor.Current = Cursors.WaitCursor;
                GeneralCommon.M_CN1.CursorLocation = ADODB.CursorLocationEnum.adUseServer;
                adoCmd = new ADODB.Command();
                adoCmd.ActiveConnection = GeneralCommon.M_CN1;
                adoCmd.CommandType      = ADODB.CommandTypeEnum.adCmdStoredProc;
                adoCmd.CommandText      = "WGA1010C.P_LOC_TUN";
                GeneralCommon.M_CN1.BeginTrans();

                for (int i = 1; i <= 2; i++)
                {
                    adoCmd.Parameters.Append(adoCmd.CreateParameter("", ADODB.DataTypeEnum.adVariant, ADODB.ParameterDirectionEnum.adParamInput, 0, null));
                }

                object value = null;
                adoCmd.Parameters.Append(adoCmd.CreateParameter("arg_e_msg", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamOutput, 256, value));
                adoCmd.Parameters[0].Value = cbocurText;
                adoCmd.Parameters[1].Value = sAddrText;
                adoCmd.Execute(out value);

                if (adoCmd.Parameters["arg_e_msg"].Value.ToString() == "")
                {
                    GeneralCommon.M_CN1.CommitTrans();
                    GeneralCommon.M_CN1.Close();
                    adoCmd         = null;
                    Cursor.Current = Cursors.Default;
                    return("");
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                    adoCmd         = null;
                    GeneralCommon.M_CN1.RollbackTrans();
                    // return "ERROR";
                }
                return("ERROR");
            }
            catch (Exception ex)
            {
                adoCmd = null;
                GeneralCommon.M_CN1.RollbackTrans();
                Cursor.Current = Cursors.Default;
                GeneralCommon.Gp_MsgBoxDisplay((string)(ex.Message), "W", "警告");
                return(ex.Message + "ERROR");
            }
        }
Ejemplo n.º 6
0
        private void cmd_Loc_Search_Click(object sender, EventArgs e)
        {
            if (txt_slab_no.Text == "")
            {
                GeneralCommon.Gp_MsgBoxDisplay("必须输入板坯号", "W", "错误提示");
                return;
            }
            string ret_Result_ErrMsg;
            int    ret_Result_ErrCode = 0;

            ADODB.Command adoCmd;
            try
            {
                if (GeneralCommon.M_CN1.State == 0)
                {
                    if (GeneralCommon.GF_DbConnect() == false)
                    {
                        return;
                    }
                }
                Cursor.Current = Cursors.WaitCursor;
                GeneralCommon.M_CN1.CursorLocation = ADODB.CursorLocationEnum.adUseServer;
                adoCmd = new ADODB.Command();
                adoCmd.ActiveConnection = GeneralCommon.M_CN1;
                adoCmd.CommandType      = ADODB.CommandTypeEnum.adCmdStoredProc;
                adoCmd.CommandText      = "AFL2010P";
                GeneralCommon.M_CN1.BeginTrans();
                for (int i = 1; i <= 2; i++)
                {
                    adoCmd.Parameters.Append(adoCmd.CreateParameter("", ADODB.DataTypeEnum.adVariant, ADODB.ParameterDirectionEnum.adParamInput, 0, null));
                }

                object value = null;
                adoCmd.Parameters.Append(adoCmd.CreateParameter("arg_e_msg1", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamOutput, 256, value));
                adoCmd.Parameters.Append(adoCmd.CreateParameter("arg_e_msg2", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamOutput, 256, value));
                adoCmd.Parameters.Append(adoCmd.CreateParameter("arg_e_msg3", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamOutput, 256, value));

                adoCmd.Parameters[0].Value = "SL";
                adoCmd.Parameters[1].Value = txt_slab_no.Text;


                adoCmd.Execute(out value);

                if (adoCmd.Parameters["arg_e_msg1"].Value.ToString().StartsWith("NOT"))
                {
                    GeneralCommon.Gp_MsgBoxDisplay("垛位查询失败,请确认", "W", "");
                    Cursor.Current = Cursors.Default;
                    adoCmd         = null;
                    GeneralCommon.M_CN1.RollbackTrans();
                }
                else
                {
                    txt_location1.Text = adoCmd.Parameters["arg_e_msg1"].Value.ToString();
                    txt_location2.Text = adoCmd.Parameters["arg_e_msg2"].Value.ToString();
                    txt_location3.Text = adoCmd.Parameters["arg_e_msg3"].Value.ToString();
                    GeneralCommon.M_CN1.CommitTrans();
                    GeneralCommon.M_CN1.Close();
                    adoCmd         = null;
                    Cursor.Current = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                adoCmd = null;
                GeneralCommon.M_CN1.RollbackTrans();
                Cursor.Current = Cursors.Default;
                GeneralCommon.Gp_MsgBoxDisplay((string)(ex.Message), "W", "警告");
            }
        }
Ejemplo n.º 7
0
    static void Main(string[] args)
    {
        ADODB.Connection conn = null;
        ADODB.Recordset  rs   = null;



        try
        {
            ////////////////////////////////////////////////////////////////////////////////
            // 连接数据源.
            //

            Console.WriteLine("正在连接数据库 ...");

            // 获取连接字符串
            string connStr = string.Format("Provider=SQLOLEDB;Data Source={0};Initial Catalog={1};Integrated Security=SSPI",
                                           ".\\sqlexpress", "SQLServer2005DB");


            // 打开连接
            conn = new ADODB.Connection();
            conn.Open(connStr, null, null, 0);


            ////////////////////////////////////////////////////////////////////////////////
            // 编写并执行ADO命令.
            // 可以是SQL指令(SELECT/UPDATE/INSERT/DELETE),或是调用存储过程.
            // 此处是一个INSERT命令示例.
            //

            Console.WriteLine("将一条记录插入表CountryRegion中...");

            // 1. 生成一个Command对象
            ADODB.Command cmdInsert = new ADODB.Command();

            // 2. 将连接赋值于命令
            cmdInsert.ActiveConnection = conn;

            // 3. 设置命令文本
            //  SQL指令或者存储过程名
            cmdInsert.CommandText = "INSERT INTO CountryRegion(CountryRegionCode, Name, ModifiedDate)"
                                    + " VALUES (?, ?, ?)";

            // 4. 设置命令类型
            // ADODB.CommandTypeEnum.adCmdText 用于普通的SQL指令;
            // ADODB.CommandTypeEnum.adCmdStoredProc 用于存储过程.
            cmdInsert.CommandType = ADODB.CommandTypeEnum.adCmdText;

            // 5. 添加参数

            //  CountryRegionCode (nvarchar(20)参数的添加
            ADODB.Parameter paramCode = cmdInsert.CreateParameter(
                "CountryRegionCode",                        // 参数名
                ADODB.DataTypeEnum.adVarChar,               // 参数类型 (nvarchar(20))
                ADODB.ParameterDirectionEnum.adParamInput,  // 参数类型
                20,                                         // 参数的最大长度
                "ZZ" + DateTime.Now.Millisecond);           // 参数值
            cmdInsert.Parameters.Append(paramCode);

            // Name (nvarchar(200))参数的添加
            ADODB.Parameter paramName = cmdInsert.CreateParameter(
                "Name",                                     // 参数名
                ADODB.DataTypeEnum.adVarChar,               // 参数类型 (nvarchar(200))
                ADODB.ParameterDirectionEnum.adParamInput,  // 参数传递方向
                200,                                        // 参数的最大长度
                "Test Region Name");                        // 参数值
            cmdInsert.Parameters.Append(paramName);

            // ModifiedDate (datetime)参数的添加
            ADODB.Parameter paramModifiedDate = cmdInsert.CreateParameter(
                "ModifiedDate",                             // 参数名
                ADODB.DataTypeEnum.adDate,                  // 参数类型 (datetime)
                ADODB.ParameterDirectionEnum.adParamInput,  // 参数传递方向
                -1,                                         // 参数的最大长度 (datetime忽视该值)
                DateTime.Now);                              // 参数值
            cmdInsert.Parameters.Append(paramModifiedDate);


            // 6. 执行命令
            object nRecordsAffected = Type.Missing;
            object oParams          = Type.Missing;
            cmdInsert.Execute(out nRecordsAffected, ref oParams,
                              (int)ADODB.ExecuteOptionEnum.adExecuteNoRecords);


            ////////////////////////////////////////////////////////////////////////////////
            // 使用Recordset对象.
            // http://msdn.microsoft.com/en-us/library/ms681510.aspx
            // Recordset表示了数据表中记录或执行命令获得的结果的集合。
            // 在任何时候, Recordset对象都指向集合中的单条记录,并将
            // 该记录作为它的当前记录。
            //

            Console.WriteLine("列出表CountryRegion中的所有记录");

            // 1. 生成Recordset对象
            rs = new ADODB.Recordset();

            // 2. 打开Recordset对象
            string strSelectCmd = "SELECT * FROM CountryRegion"; // WHERE ...
            rs.Open(strSelectCmd,                                // SQL指令/表,视图名 /
                                                                 // 存储过程调用 /文件名
                    conn,                                        // 连接对象/连接字符串
                    ADODB.CursorTypeEnum.adOpenForwardOnly,      // 游标类型. (只进游标)
                    ADODB.LockTypeEnum.adLockOptimistic,         // 锁定类型. (仅当需要调用
                                                                 // 更新方法时,才锁定记录)
                    (int)ADODB.CommandTypeEnum.adCmdText);       // 将第一个参数视为SQL命令
                                                                 // 或存储过程.

            // 3. 通过向前移动游标列举记录

            // 移动到Recordset中的第一条记录
            rs.MoveFirst();
            while (!rs.EOF)
            {
                // 当在表中定义了一个可空字段,需要检验字段中的值是否为DBNull.Value.
                string code = (rs.Fields["CountryRegionCode"].Value == DBNull.Value) ?
                              "(DBNull)" : rs.Fields["CountryRegionCode"].Value.ToString();

                string name = (rs.Fields["Name"].Value == DBNull.Value) ?
                              "(DBNull)" : rs.Fields["Name"].Value.ToString();

                DateTime modifiedDate = (rs.Fields["ModifiedDate"].Value == DBNull.Value) ?
                                        DateTime.MinValue : (DateTime)rs.Fields["ModifiedDate"].Value;

                Console.WriteLine(" {2} \t{0}\t{1}", code, name, modifiedDate.ToString("yyyy-MM-dd"));

                // 移动到下一条记录
                rs.MoveNext();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("应用程序出现错误: {0}", ex.Message);
            if (ex.InnerException != null)
            {
                Console.WriteLine("描述: {0}", ex.InnerException.Message);
            }
        }
        finally
        {
            ////////////////////////////////////////////////////////////////////////////////
            // 退出前清理对象.
            //

            Console.WriteLine("正在关闭连接 ...");

            // 关闭record set,当它处于打开状态时
            if (rs != null && rs.State == (int)ADODB.ObjectStateEnum.adStateOpen)
            {
                rs.Close();
            }

            // 关闭数据库连接,当它处于打开状态时
            if (conn != null && conn.State == (int)ADODB.ObjectStateEnum.adStateOpen)
            {
                conn.Close();
            }
        }
    }