请帮我,我得到错误con.open

问题描述:

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.Configuration;
using System.Data.SqlClient;

namespace login
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection("data source=E:\\Jegan Works\\c sharp\\login\\login\\login.sdf;initial catalog=login;Integrated security=true");
        SqlCommand cmd;

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            cmd=new SqlCommand();
            cmd.Connection=con;
            cmd.CommandType=CommandType.Text ;
            cmd.CommandText="insert into login(username,password) values(@username,@password)";
            cmd.Parameters.AddWithValue("@username", username.Text);
            cmd.Parameters.AddWithValue("@password", password.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("record inserted successfully");

        }
    }
}

您好,



你正在使用SqlServer Compact Edition(.sdf),所以你怎么能期望使用SqlConnection打开连接。





与Compact Edition Service连接你必须使用System.Data.SqlServerCe添加

Hello ,

You are using SqlServer Compact Edition (.sdf) ,so how could you expect to open the connection using SqlConnection .


to connection with Compact Edition Service you must add
using System.Data.SqlServerCe



这个命名空间。并使用


this namespace .and use

SqlCeConnection ,SqlCeDataAdapter

而不是

SqlConnection,SqlCommand,and SqlDataReader



最后但并非最不重要的是在webconfig文件中添加此提供者名称


last but not the least in the webconfig file add this providername

providerName="Microsoft.SqlServerCe.Client.3.5"

如果您正在使用SqlserverCe 3.5





if you are using SqlserverCe 3.5


SqlCeConnection con = new SqlCeConnection("data source=E:\\Jegan Works\\c sharp\\login\\login\\login.sdf;initial catalog=login;");
con.Open();
//other codes...
con.Close()





谢谢



thanks


你没有用SqlConnection打开一个SDF文件:你需要一个SqlCeConnection。

同样,你需要SqlCeCommand,SqlCeReader等等。
You don't open an SDF file with an SqlConnection: you need an SqlCeConnection instead.
Similarly, you will need SqlCeCommand, SqlCeReader and so forth as well.