请帮我,我得到错误con.open [英] please help me i got the error con.open

查看:184
本文介绍了请帮我,我得到错误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.


这篇关于请帮我,我得到错误con.open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆