C#用户设置界面(一):SQL如何查找利用返回值

一,初始化数据库连接

define是类,定义全局变量

Define.connectstring = "Data Source=105.147.202.11;Initial Catalog=clearer;User ID=**;Password=*************.";
Define.sqlConnection = new SqlConnection(Define.connectstring);

二,确认数据库是否打开

 if (Define.sqlConnection.State.Equals(ConnectionState.Closed))//判断当前数据库状态,是否打开
                    Define.sqlConnection.Open();


三,写入SQL语句命令
SqlCommand SqlCom = new SqlCommand(); //新建了命令类
string execstring;
execstring = "select 用户密码 from table1 用户名='" + username + "'"; //SQL控制语句,username是外部变量
SqlCom.Connection = Define.sqlConnection; //连接地址
SqlCom.CommandText = execstring; //控制命令
SqlCom.ExecuteNonQuery(); //执行命令
SqlDataReader dataReader = SqlCom.ExecuteReader(); //定义读取流
                    if (dataReader.Read())
                    {
                        password = Convert.ToString(dataReader[0]); //读取返回数组第一列,即用户密码列第一个
                        dataReader.Close();
                        Define.sqlConnection.Close(); //关闭
                        return password; //返回获得的用户密码
                    }
                    else
                    {
                        dataReader.Close();
                        Define.sqlConnection.Close();
                        password = "查找无此用户";
                        return password;
                    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值