将SQL数据库连接到C#

时间:2011-12-02 18:18:04

标签: c# sql

我对SQL有一点问题,它说“public Form1();在代码的最顶端,根据我正在关注的begginers指南,应该有一个带有3个点的小盒子(像这[...])而不是“;”

public Form1();

        System.Data.SqlClient.SqlConnection con;

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();

            con.ConnectionString="Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Wee                                                                    Jimmy\\Documents\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            con.Open();

            MessageBox.Show("Open");

            con.Close();

            MessageBox.Show("Closed");

请有人帮忙我准备好了。错误说,错误1'SQL_Connection.Form1.Form1()'必须声明一个正文,因为它没有标记为abstract,extern或partial C:\用户\ Wee Jimmy \ Documents \ Visual Studio 2010 \ Projects \ SQL Connection \ SQL Connection \ Form1.cs 15 16 SQL连接

2 个答案:

答案 0 :(得分:1)

更改第一行:

public Form1();

为:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}

答案 1 :(得分:0)

试试这个 -

public Form1()
{
    System.Data.SqlClient.SqlConnection con; 

    private void Form1_Load(object sender, EventArgs e) 
    { 
        con = new System.Data.SqlClient.SqlConnection(); 

        con.ConnectionString="Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Wee Jimmy\\Documents\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; 

        con.Open(); 

        MessageBox.Show("Open"); 

        con.Close(); 

        MessageBox.Show("Closed"); 
    }
}