SelectAllStu GetNameById queryStuById

来源:互联网 发布:feynman 知乎 编辑:程序博客网 时间:2024/06/01 11:17


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;namespace SelectAllStu{    class Program    {        public static SqlConnection conn;        static void Main(string[] args)        {            ConnectionDb();            //UpdateCommand();            //Console.WriteLine(SelectCommand());            SelectCommand();            GetNameById("01");            GetNameById("03");            GetNameById("10");            queryStuById("01");            queryStuById("03");            queryStuById("10");            //Console.ReadLine();        }        ///<summary>        /// 数据库连接        ///</summary>        private static void ConnectionDb()        {            /*            */            conn = new SqlConnection("UID=sa;Password=123456;Initial Catalog=Test;Data Source=127.0.0.1");            if (conn.State == ConnectionState.Closed)             {                 conn.Open();             }         }        ///<summary>        /// 数据库查询        ///</summary>        ///<returns>返回查询结果</returns>        private static void SelectCommand()        {            //var strCmd = "SELECT [S#],[Sname],[Sage],[Ssex] FROM [student]";            //sqlComm = new SqlCommand("queryStuNameById", sqlCon);            //             try            {                //SqlCommand sqlComm = new SqlCommand();                                //sqlComm.Connection = conn;                //sqlComm.CommandText = strCmd;                SqlCommand sqlComm = new SqlCommand("SelectAllStu", conn);                sqlComm.CommandType = CommandType.StoredProcedure;//设置命令的类型为存储过程                /*                var abc = sqlComm.ExecuteNonQuery();                if (true || abc != -1)                {                    Console.WriteLine("sqlComm.ExecuteNonQuery= {0}",abc);                }                */                bool showForm = true;                if (showForm)                {                    //将Command对象作为DataAdapter的参数传进                    SqlDataAdapter da = new SqlDataAdapter(sqlComm);                    DataSet ds = new DataSet();                    da.Fill(ds);                    Form1 form = new Form1();                    form.setDataSet(ds);                    //form.Show();                    form.ShowDialog();                    return;                }                SqlDataReader dataReader = sqlComm.ExecuteReader();                while (dataReader.Read())                {                    //return "机器IP:" + dataReader["IP"] + "\t" + "监控状态:" + dataReader["IsWatch"];                     Console.WriteLine("{0},{1},{2},{3}",                         dataReader["S#"],dataReader["Sname"],dataReader["Sage"],dataReader["Ssex"]);                }                 //Console.WriteLine("没找到任何信息!");                dataReader.Close();//关闭执行                //con.Close();//关闭数据库            }            catch (Exception ex)            {                Console.WriteLine("出异常了:" + ex.Message);            }        }        private static void queryStuById(String strId)        {                        try            {                SqlCommand sqlComm = new SqlCommand("queryStuById", conn);                sqlComm.CommandType = CommandType.StoredProcedure;//设置命令的类型为存储过程                //设置参数                sqlComm.Parameters.Add("@stuId", SqlDbType.VarChar);                //为参数赋值                sqlComm.Parameters["@stuId"].Value = strId;                SqlDataReader dataReader = sqlComm.ExecuteReader();                while (dataReader.Read())                {                    Console.WriteLine("{0},{1},{2},{3}",                        dataReader["S#"], dataReader["Sname"], dataReader["Sage"], dataReader["Ssex"]);                }                //Console.WriteLine("没找到任何信息!");                dataReader.Close();//关闭执行                //con.Close();//关闭数据库            }            catch (Exception ex)            {                Console.WriteLine("出异常了:" + ex.Message);            }        }        private static void GetNameById(String strId)        {            SqlCommand sqlComm = new SqlCommand("queryStuNameById1", conn);            //设置命令的类型为存储过程            sqlComm.CommandType = CommandType.StoredProcedure;            //设置参数            SqlParameter sp = sqlComm.Parameters.Add("@stuId1", SqlDbType.VarChar);            //注意输出参数要设置大小,否则size默认为0,            sqlComm.Parameters.Add("@stuName1", SqlDbType.NVarChar, 10);            //设置参数的类型为输出参数,默认情况下是输入,            sqlComm.Parameters["@stuName1"].Direction = ParameterDirection.Output;            //为参数赋值            //sqlComm.Parameters["@stuId1"].Value = strId;            sp.Value = strId;            //执行            sqlComm.ExecuteNonQuery();            //得到输出参数的值,把赋值给name,注意,这里得到的是object类型的,要进行相应的类型轮换            String name = sqlComm.Parameters["@stuName1"].Value.ToString();            if(name!="")               Console.WriteLine("{0}:{1}",strId,name);            else                Console.WriteLine("{0}:{1}(empty)", strId, name);        }        private static bool UpdateCommand()        {            string strCmd = "update [SmokeTest].[dbo].[Machine]" +            "set [IsWatch] = \'1\'" +            "where IP = \'192.168.*.*\'";            try            {                SqlCommand sqlComm = new SqlCommand();                sqlComm.Connection = conn;                sqlComm.CommandText = strCmd;                sqlComm.ExecuteNonQuery();                return true;            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                return false;            }        }    }}


0 0
原创粉丝点击