SelectAllStu GetNameById queryStuById


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;
            }
        }

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值