c#vs连接SQL sever数据库入门操作

对于需要连接数据库的项目,可以参考的简单初级代码。实现打开数据库,读入数据功能

 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 
11 namespace SQL_sever
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18         }
19 
20         private void button1_Click(object sender, EventArgs e)
21         {
22             string Eid = txtID.Text;
23             string PName = txtPro.Text;
24             string EOut = Money.Text;
25             SqlConnection Sql = new SqlConnection();
26             Sql.ConnectionString = "server=.;database=风投数据;uid=sa;pwd=123";//根据本机地址,以超级管理员身份进入
27             Sql.Open();
28             SqlCommand cmd = new SqlCommand();
29             cmd.Connection = Sql;
30             cmd.CommandType = CommandType.Text;
31 
32             cmd.CommandText = "insert into Project values('"+Eid+"',"+PName+","+EOut+")";
33             SqlDataReader dr = cmd.ExecuteReader();//执行SQL语句
34             dr.Close();//关闭执行
35             Sql.Close();//关闭数据库
36         }
37     }
38 }

 

posted @ 2018-08-06 17:11  大魏御宇  阅读(415)  评论(0)    收藏  举报