SqlCe中的conn.Open()错误 [英] conn.Open() error in SqlCe
问题描述
当我运行以下代码时它不会打开连接一段时间显示错误连接字符串也显示系统提供的路径错误不正确
使用系统;
使用System.Linq;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Data.SqlServerCe;
使用System.Drawing;
使用System.Text;
使用System.Windows.Forms;
命名空间Master
{
公共部分课程Form1:表格
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
}
private void tx1_TextChanged(对象发送者,E ventArgs e)
{
}
private void tx2_TextChanged(object发件人,EventArgs e)
{
}
private void sn_Click (对象发送者,EventArgs e)
{
SqlCeConnection conn = new SqlCeConnection();
conn。 ConnectionString =数据源= C:\\Projects \\ Master \\Master \\sanjay.sdf;
conn。 Open();
// SqlCeDataReader dr = null;
SqlCeCommand cmd = new SqlCeCommand(INSERT INTO emp(usid,pass)VALUES(@ us,@ id) ,conn);
cmd.Parameters.Add(new SqlCeParameter(@ ns,tx1.Text));
cmd.Parameters.Add(new SqlCeParameter( @id,tx2.Text));
//qw.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show(成功插入数据);
tx1.Text =;
tx2.Text =;
}
private void can_Click(object发件人,EventArgs e)
{
Application.Exit();
}
}
}
while i run the below code it will not open the connection some time show error in Connection String also show error of Path provided by system in not correct
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlServerCe;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Master
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void tx1_TextChanged(object sender, EventArgs e)
{
}
private void tx2_TextChanged(object sender, EventArgs e)
{
}
private void sn_Click(object sender, EventArgs e)
{
SqlCeConnection conn = new SqlCeConnection();
conn.ConnectionString ="Data Source=C:\\Projects\\Master\\Master\\sanjay.sdf";
conn.Open();
//SqlCeDataReader dr = null;
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO emp(usid,pass) VALUES (@us,@id)",conn);
cmd.Parameters.Add(new SqlCeParameter("@ns", tx1.Text));
cmd.Parameters.Add(new SqlCeParameter("@id", tx2.Text));
//qw.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Insert Data Sucessfully");
tx1.Text = "";
tx2.Text = "";
}
private void can_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
推荐答案
阅读以下内容: http://www.connectionstrings.com/sql-server-compact/ [ ^ ]
Read the following : http://www.connectionstrings.com/sql-server-compact/[^]
检查连接字符串
假设使用mdf文件意味着你在给出连接字符串
Suppose using mdf file mean you can restore mdf file in databse after give you connection string
这篇关于SqlCe中的conn.Open()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!