连接到数据库时出现问题 [英] Problem when connecting to a database

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

问题描述

嘿伙计们,我有一个问题..我刚刚开始学习数据库,现在指南说我应该连接到我的数据库等,我写的代码应该像这样:



Hey guys, I have a problem.. I''ve just start to learn about databases a bit and now the guide says that i should connect to my database etc and i wrote the code as it should like this:

<pre lang="cs">System.Data.SqlServerCe.SqlCeConnection con;

private void Form1_Load(object sender, EventArgs e)
{
    con = new System.Data.SqlServerCe.SqlCeConnection();
    con.ConnectionString = "Data Source=C:\\Users\\Owner\\Documents\\Databases\\Employees.sdf";
    con.Open();

    MessageBox.Show("Open");

    con.Close();
}







消息框是告诉我它是否正确打开但我只是当我按下这样的按钮时出现错误:SqlCeException未处理System.Data.SqlServerCe.dll中出现未处理的类型'System.Data.SqlServerCe.SqlCeException'异常



我也尝试将所有者更改为我自己的名字并且它工作了一次,但是当我改为所有者时它没有工作然后我再次更改为我的名字然后它没有工作吗?我应该怎么做才能使这项工作?因为我也希望其他用户能够使用我的程序,如果有人下载它,数据库也应该适用于他们。




The messagebox is there to tell me if it opens correctly but i just get an error when i press the button saying something like this: "SqlCeException was unhandled" "An unhandled exception of type ''System.Data.SqlServerCe.SqlCeException'' occurred in System.Data.SqlServerCe.dll"

I also tried to change Owner to my own name and it worked one time, but when i then changed to Owner it didn''t work and then i changed to my name again and then it DOESN''T work? What should i do to make this work? Cause i also want other users to be able to use my program, if anyone download it the database should work for them too.

推荐答案

看到这个以获得正确的连接字符串: http://connectionstrings.com/ [ ^ ]



更多:

初学者指南,通过C#访问SQL Server [ ^ ]

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

http://msdn.microsoft.com/en-US/library/system.data.sqlclient。 sqlcommand%28v = vs.80%29.aspx [ ^ ]

http://msdn.microsoft.com/en-US/library/system.data.sqlclient.sqldatareader%28v=vs.80 %29.aspx [ ^ ]
See this to get proper connection string: http://connectionstrings.com/[^]

More:
Beginners guide to accessing SQL Server through C#[^]
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection%28v=vs.80%29.aspx[^]
http://msdn.microsoft.com/en-US/library/system.data.sqlclient.sqlcommand%28v=vs.80%29.aspx[^]
http://msdn.microsoft.com/en-US/library/system.data.sqlclient.sqldatareader%28v=vs.80%29.aspx[^]


这篇关于连接到数据库时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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