VS2008连接数据库

来源:互联网 发布:视觉盛宴软件下载 编辑:程序博客网 时间:2024/05/16 04:11

   做毕业设计,想把数据存放在VS自带的数据库里面(为了方便,没有单独装SQL),只想用Gredview进行图标展示,仅此而已。

   数据连接--->添加连接

连接数据库,连了半天,终于搞定。MARK一下。

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 WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 这个是窗体的构造函数  基本的数据初始化  是在这个函数里面进行的   而非在窗体加载事件
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            this.Init();
        }

     

        private void Init()
        {
           SqlConnection sqlCnn=new SqlConnection(@"Data Source=.\SQLEXPRESS;Integrated Security=True");
           sqlCnn.Open();
           //string sql = "insert into comment(newsid,[content],userip) values(@newsID,@content,@userIP)";
           string sql="insert into Users (Name,password) values('aaa','bbbb')";
           SqlCommand sqlCmd=new SqlCommand(sql,sqlCnn);
           SqlDataAdapter sda=new SqlDataAdapter(sqlCmd);
           DataTable table=new DataTable();
           sda.Fill(table);
           this.dataGridView1.DataSource=table;
           sqlCnn.Close();
        }

   
    }
}