Chinaunix首页 | 论坛 | 博客
  • 博客访问: 540691
  • 博文数量: 156
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1183
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-22 11:42
文章分类

全部博文(156)

文章存档

2015年(67)

2014年(89)

分类: C#/.net

2014-11-30 20:26:36


  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient; //引用的数据库空间

  10. namespace SQL
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         SqlConnection MyConnect = new SqlConnection();

  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }

  19.         private void LinkDatabase_Click(object sender, EventArgs e)
  20.         {
  21.             MyConnect.ConnectionString = "Data Source =(local); Initial Catalog = yyj; User ID = yangyanjie; Password = 123456"; //中            间不能有任何引号
  22.             MyConnect.Open();
  23.             MessageBox.Show("打开数据库成功");
  24.         }

  25.         private void CreateTable_Click(object sender, EventArgs e)
  26.         {
  27.             SqlCommand CreateTableCmd = new SqlCommand();
  28.             CreateTableCmd.Connection = MyConnect;
  29.             CreateTableCmd.CommandType = CommandType.Text;
  30.             CreateTableCmd.CommandText = "CREATE TABLE Teacher(Tea_ID nchar(10) NOT NULL Primary Key, Name nchar(20) NOT NULL, Sex nc            har(8) NOT NULL, Address nchar(40) NOT NULL)";
  31.             // CreateTableCmd.ExecuteScalar();
  32.             CreateTableCmd.CommandText = "CREATE TABLE Student(Stu_ID nchar(10) NOT NULL Primary Key, Name nchar(20) NOT NULL, Sex nc            har(8) NOT NULL, Phone nchar(18) NOT NULL, Address nchar(40) NOT NULL)";
  33.              // CreateTableCmd.ExecuteScalar();
  34.             CreateTableCmd.CommandText = "CREATE TABLE Money(Stu_ID nchar(10) NOT NULL Primary Key, Name nchar(20) NOT NULL, Phone nc            har(18) NOT NULL, Year money NOT NULL)";
  35.             // CreateTableCmd.ExecuteScalar();
  36.             CreateTableCmd.CommandText = "CREATE TABLE Subject(Sub_ID nchar(10) NOT NULL Primary Key, Sub_Name nchar(20) NOT NULL, Sc            ore smallint NOT NULL)";
  37.             CreateTableCmd.ExecuteScalar();
  38.             CreateTableCmd.CommandText = "CREATE TABLE AddrBook(Name nchar(20) NOT NULL Primary Key, Score smallint NOT NULL, Phone n            char(18) NOT NULL, QQ nchar(15) NOT NULL, Address nchar(40) NOT NULL)";
  39.             CreateTableCmd.ExecuteScalar();

  40.             MessageBox.Show("创建表格成功");
  41.         }

  42.         private void AlterTable_Click(object sender, EventArgs e)
  43.         {
  44.             SqlCommand CreateTableCmd = new SqlCommand();
  45.             CreateTableCmd.Connection = MyConnect;
  46.             CreateTableCmd.CommandType = CommandType.Text;
  47.             CreateTableCmd.CommandText = "ALTER TABLE add name_123 nchar(18)";
  48.             CreateTableCmd.ExecuteScalar();

  49.             MessageBox.Show("修改表格成功");
  50.         }

  51.         private void DropTable_Click(object sender, EventArgs e)
  52.         {
  53.             SqlCommand CreatetableCmd = new SqlCommand();
  54.             CreatetableCmd.Connection = MyConnect;
  55.             CreatetableCmd.CommandType = CommandType.Text;
  56.             CreatetableCmd.CommandText = "DROP TABLE dd";
  57.             CreatetableCmd.ExecuteScalar();

  58.             MessageBox.Show("删除表格成功");
  59.         }

  60.         private void CloseDatabase_Click(object sender, EventArgs e)
  61.         {
  62.             MyConnect.Close();
  63.             MessageBox.Show("关闭数据库成功");
  64.         }
  65.     }
  66. }

阅读(1239) | 评论(0) | 转发(0) |
1

上一篇:用C#画圆

下一篇:计算器

给主人留下些什么吧!~~