您的位置:首页 > 数据库

关于ADO.NET-创建一个登陆窗口 连接SQLServer2000

2007-10-18 17:23 435 查看
这里主要用到了System.Data.Sqlclient对象

连接首先创建一个Connection连接
然后使用数据命令
今天作的例子很简单 没有用复杂的数据命令
代码发在这里

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ObjectTest
{
public partial class Form1 : Form
{
public SqlConnection Connect()
{
//建立数据库连接
SqlConnection SqlDbConnection1 = new SqlConnection("Server=localhost;Database=sqldb_test;uid=sa;pwd=rickey");
//SqlDbConnection1.ConnectionString = "Server=localhost;Database=sqldb_test;uid=;pwd=";
SqlDbConnection1.Open();
return SqlDbConnection1;
}

public Form1()
{
InitializeComponent();
this.Connect();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
//使用数据命令 操作数据库 对数据库进行查询
String u_name = textBox1.Text;
String u_password = textBox2.Text;
SqlConnection SqlDbConnection = Connect();
String sqlstr = "select * from user_check where u_name = '" + u_name + "' and u_password = '" + u_password + "'";
SqlCommand scd = new SqlCommand(sqlstr, SqlDbConnection);
if (scd.ExecuteScalar() == null)
{
MessageBox.Show("用户名或密码错误");
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
else
{
MessageBox.Show("登陆成功");
}
}

private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
}
}

很高兴 连接到了数据库
好象以前学其他开发平台的时候就觉得连接上了数据库
就没有什么再难的了
其实并不是这样的
以后要学的细节的东西还很多

要加油!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐