using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //连接数据库的头文件
//有错!文件打开失败
namespace WinFormSQL_exe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
String name = this.textBox1.Text;
String password = this.textBox2.Text;
//获得数据库连接字符串Data Source=.;Initial Catalog=winformSQL;Integrated Security=True
String conn = "Data Source=.;Initial Catalog=winformSQL;Integrated Security=True";
//创建SqlConnection
SqlConnection connect = new SqlConnection(conn);
connect.Open();
String Sql=String.Format("Selecr count(8) form Londing where usemer ='{0}'and password='{1}'",name,password);
//创建SqlCommand对象
SqlCommand command = new SqlCommand(Sql, connect);
int num = Convert.ToInt32(command.ExecuteScalar());
try
{
if (num > 0)
{
MessageBox.Show("登陆成功!");
}
else
{
MessageBox.Show("账户或密码错误!");
}
}
catch(Exception ex)
{
MessageBox.Show("异常:"+ex);
}
finally
{
connect.Close();
}
}
}
}
数据库我还没有学过,所以一些基本的我也不知道,我只是想自学一下连接数据库,那么请问怎么连接数据库呢?
|