工具:VS2013
数据库SqlServer2008
两张表,一个用户登录表,一个资料表用于增删改查 。先把表建好。可以根据我发的图建立,这样下面的代码修改的就少。
资料部分SQL
CREATE TABLE [dbo].[Customer](
[CustomerID] [varchar](20) NOT NULL,
[CompanyName] [varchar](20) NULL,
[ContacName] [varchar](20) NULL,
[ContacTitle] [nchar](10) NULL,
[Address] [nchar](10) NULL,
[City] [nchar](10) NULL,
[Region] [nchar](10) NULL,
[PostalCode] [nchar](10) NULL,
[Country] [nchar](10) NULL,
[Phone] [nchar](10) NULL,
[Fax] [nchar](10) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
一、新建项目
二、拖入控件,自己拖入,不用说了。
三、双击登录按钮,在双击方法里面写入下面代码。(form2是登录成功跳转到的页面,顺便新建一个就好了。里面有输入框什么的名称报错,自己修改修改。改成和我一样的就可以了。)
最上面加入下面的引用
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;
private void button1_Click(object sender, EventArgs e)
{
string uername = textBox1.Text.Trim();
string pwd = textBox2.Text;
// 建立SqlConnection对象,根据你的数据库,用户名和密码修改一下
SqlConnection con = new SqlConnection("server=LAPTOP-SVLMBJT3;database=Northwind;user=sa;pwd=sa");
// 打开连接
con.Open();
// 指定SQL语句,我是数据库的一张表,用来测试登录,自己建立一个表,
SqlCommand com = new SqlCommand("SELECT USERNAME,PASSWORD form tb_MYUSER where USERNAME='" + uername + "' and PASSWORD='" + pwd + "'", con);
// 建立SqlDataAdapter和DataSet对象
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
int n = da.Fill(ds, "tb_MYUSER