using System.Data.SqlClient //包含访问数据库的类
SqlConnection myCon = new SqlConnection();//说明一个SqlConnection类的实例
myCon.ConnectionString = "Persist Security Info=False;User id=sa;pwd=123;database=demo;server=(local)";//设置打开数据库的参数。Data Source:要连接的SQL实例名称或网络地址;若要连接到本地机器,可将服务器指定为“(local)”.
myCon.Open();
使用SqlCommandH获取查询语句:
SqlCommand selectCMD=new SqlCommand();
selectCMD.Connection=myCon;
selectCMD.CommandText="SELECT `````FORM````";
使用SqlCommandH提交非查询(增删改)语句:
SqlCommand selectCMD=new SqlCommand();
selectCMD.Connection=myCon;
selectCMD.CommandText="UPDATE Customers SET ``=```````";
int i=selectCMD.ExecuteNonQuery();//对连接执行 非查询的SQL语句并返回受影响的行数
使用DataAdapter提交查询命令:
SqlCommand selectCMD = new SqlCommand("SELECT*FROM t1 WHERE id='" + name + "'", myCon);//构造连接字符串提交增删改命令(DBCommand)
SqlDataAdapter custDa = new SqlDataAdapter();//获取数据适配器
custDa.SelectCommand = selectCMD;
//提交查询,获取结果数据集
DataSet custDs = new DataSet();//DataSet:管理数据
custDa.Fill(custDs);
System.String name = textBox_LoginName.Text;
System.String password = textBox_Password.Text;
int i = custDs.Tables[0].Rows.Count; //返回内存中缓存查询结果数据表的个数。
if (i == 0)
{
MessageBox.Show("用户名不存在!");
Exit_Sys = true;
}
else
{
System.String value = custDs.Tables[0].Rows[0][1].ToString();//返回指定表和制定表位置的字符
if ( String.Equals(value,password))//比较字符串
{
MessageBox.Show("密码错误!");
Exit_Sys = true;
}
else Close();
}
myCon.Close();
附录:
控件焦点问题:
1,在一个表单里tabindex属性为0的控件,当这个表单启动时,会自动首先获得焦点.然后按tab和enter键,会依次让其它控件获得焦点.
2,一个控件获取焦点的方法:control.Focus();