51与PC通信协议设计及实现(七):关于数据库

按前面的要求,所应用到数据库为sql serve2005,经过我三天的摸索学习,发现数据库还是非常简单的哈~~

先上个学习过程写的数据库,都是最最简单的操作,包括数据库的连接,增减字段……嘿嘿,这么简单竟然学了那么久……惭愧

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

namespace WindowsApp_sql
{
public partial class Form1 : Form
{
SqlConnection sqlconn = new SqlConnection();
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
open_conn(sender,e);
if (sqlconn.State == ConnectionState.Open)
{
test_conn.Text = "连接成功";
test_conn.Enabled = false;
exit.Enabled = true;
add.Enabled = true;
re_data.Enabled = true;
// sqlconn.Close();
}
else test_conn.Text = "连接失败";
}

private void add_Click(object sender, EventArgs e)
{
//open_conn(sender,e);
string add = "INSERT INTO score VALUES('5','数据结构',88)";
SqlCommand sc = new SqlCommand();
sc.CommandText = add;
sc.Connection = sqlconn;
sc.ExecuteNonQuery();
}

private void open_conn(object sender, EventArgs e)
{
string myconn = "Server = 192.168.47.128;uid = sa;pwd =;initial catalog = mydb;Connect Timeout=30";
sqlconn.ConnectionString = myconn;
try
{
sqlconn.Open();
}
catch (SqlException se)
{
MessageBox.Show(se.ToString());
}
}


private void re_data_Click(object sender, EventArgs e)
{
string add = "DELETE FROM score WHERE score.学号 = 5";
SqlCommand sc = new SqlCommand();
sc.CommandText = add;
sc.Connection = sqlconn;
sc.ExecuteNonQuery();
}

private void exit_Click(object sender, EventArgs e)
{
if (sqlconn.State == ConnectionState.Open)
{
sqlconn.Close();
exit.Text = "已断开连接";
exit.Enabled = false;
test_conn.Enabled = true;
add.Enabled = false;
re_data.Enabled = false;
test_conn.Text = "连接数据库";
}
}

private void Form1_Load(object sender, EventArgs e)
{
if (sqlconn.State == ConnectionState.Open)
{
exit.Enabled = true;
add.Enabled = true;
re_data.Enabled = true;
}
else
{
exit.Enabled = false;
add.Enabled = false;
re_data.Enabled = false;
}
}
}
}

数据库的基本操作还是非常简单的……

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

namespace WindowsApp_sql
{
public partial class Form1 : Form
{
SqlConnection sqlconn = new SqlConnection();
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
open_conn(sender,e);
if (sqlconn.State == ConnectionState.Open)
{
test_conn.Text = "连接成功";
test_conn.Enabled = false;
exit.Enabled = true;
add.Enabled = true;
add_base.Enabled = true;
add_table.Enabled = true;
re_data.Enabled = true;
// sqlconn.Close();
}
else test_conn.Text = "连接失败";
}

private void add_Click(object sender, EventArgs e)
{
//open_conn(sender,e);
string add = "INSERT INTO score VALUES('5','数据结构',88)";
SqlCommand sc = new SqlCommand();
sc.CommandText = add;
sc.Connection = sqlconn;
sc.ExecuteNonQuery();
}

private void open_conn(object sender, EventArgs e)
{
string myconn = "Server = 192.168.47.128;uid = sa;pwd =;initial catalog = mydb;Connect Timeout=30";
sqlconn.ConnectionString = myconn;
try
{
sqlconn.Open();
}
catch (SqlException se)
{
MessageBox.Show(se.ToString());
}
}


private void re_data_Click(object sender, EventArgs e)
{
string add = "DELETE FROM score WHERE score.学号 = 5";
SqlCommand sc = new SqlCommand();
sc.CommandText = add;
sc.Connection = sqlconn;
sc.ExecuteNonQuery();
}

private void exit_Click(object sender, EventArgs e)
{
if (sqlconn.State == ConnectionState.Open)
{
sqlconn.Close();
exit.Text = "已断开连接";
exit.Enabled = false;
test_conn.Enabled = true;
add.Enabled = false;
add_base.Enabled = false;
add_table.Enabled = false;
re_data.Enabled = false;
test_conn.Text = "连接数据库";
}
}

private void Form1_Load(object sender, EventArgs e)
{
if (sqlconn.State == ConnectionState.Open)
{
exit.Enabled = true;
add.Enabled = true;
add_base.Enabled = true;
add_table.Enabled = true;
re_data.Enabled = true;
}
else
{
exit.Enabled = false;
add.Enabled = false;
add_table.Enabled = false;
add_base.Enabled = false;
re_data.Enabled = false;
}
}

private void add_base_Click(object sender, EventArgs e)
{
string add = "CREATE DATABASE keys";
SqlCommand sc = new SqlCommand();
sc.CommandText = add;
sc.Connection = sqlconn;
sc.ExecuteNonQuery();
}

private void add_table_Click(object sender, EventArgs e)
{
string add = "CREATE TABLE keys ("+
"按键位置 CHAR(10),"+
"按键状态 CHAR(10),"+
"时间 CHAR(10),"+
"次数 CHAR(10)"
+")";
SqlCommand sc = new SqlCommand();
sc.CommandText = add;
sc.Connection = sqlconn;
sc.ExecuteNonQuery();
}
}
}


我的函数和变量的命名规则有问题,得规范起来!!!



posted @ 2012-06-04 15:19  SimArchitect  阅读(203)  评论(0编辑  收藏  举报