sql数据库压缩3.5 [英] sql database compact 3.5

查看:94
本文介绍了sql数据库压缩3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在我的表单应用程序中,我已经在Visual Studio 2008中使用了sql database compact 3.5.在通过查询在表中插入数据时,它在打开连接时产生错误,并提示未找到数据源.
我该怎么办?

代码:

hello,

in my form application i have used sql database compact 3.5 with visual studio 2008.. at the inserting data in the table by query, it generates error at opening of the connection and also prompts that the datasource is not found..

what should i do?

Code :

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.Odbc;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static string ConStr = @"Data Source=C:\Users\Rohit\Desktop\WindowsFormsApplication1\MyDatabase#1.sdf";
        OdbcConnection con = new OdbcConnection(ConStr);

        public Form1()
        {
            InitializeComponent();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            string InsStr = "insert into student values ('"+textBox1.Text+"','"+textBox2.Text+"')";
            OdbcDataAdapter da = new OdbcDataAdapter(InsStr, con);
            DataTable dt = new DataTable("student");
            da.Fill(dt);
            MessageBox.Show("Record inserted succesfully...");
        }
    }
}

推荐答案

对于SQLCE,您需要使用SqlCeXXX类:
For SQLCE you need to use the SqlCeXXX classes:
SqlCeConnection con = new SqlCeConnection(ConStr);

private void addBtn_Click(object sender, EventArgs e) 
{ 
   string InsStr = "insert into student values '"+textBox1.Text+"','"+textBox2.Text+"')"; 
   SqlCeDataAdapter da = new SqlCeDataAdapter (InsStr, con); 
   DataTable dt = new DataTable("student"); 
   da.Fill(dt); 
   MessageBox.Show("Record inserted succesfully..."); 
}


另外,在使用连接之前,必须先Open()连接.

http://msdn.microsoft.com/zh-cn/library/system.data.sqlserverce.sqlceconnection%28v = vs.80%29.aspx [


Also you must Open() the connection before using it.

http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection%28v=vs.80%29.aspx[^]


不要" t使用ODBC连接,请改用SqlCeConnection:
Don''t use an ODBC connection, use an SqlCeConnection instead:
using (SqlCeConnection con = new SqlCeConnection(ConnectionString))
    {
    con.Open();
    using (SqlCeCommand cmd = new SqlCeCommand("SELECT iD, userName FROM MyTable", con))
        {
        SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
        da.Fill(dt);
        }
    }

您可能需要先下载它( http://www.microsoft.com. com/en-us/download/details.aspx?id = 5783 [ ^ ]),然后添加对"System.Data.SqlCE.dll"的引用

You may need to download it first (http://www.microsoft.com/en-us/download/details.aspx?id=5783[^]) and add a reference to "System.Data.SqlCE.dll"


这篇关于sql数据库压缩3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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