如何连接本地SQL数据库?

时间:2013-07-23 09:01:37

标签: c# sql sql-server database

到目前为止,我已经获得了一个读取XML文件并以图形方式显示这些文件的软件,以便向用户提供这些文件描述的事件的概述。

截至目前,关键是使用该软件的工程师之一要求您加载到程序中的所有数据都可以保存到数据库中。

我的泡菜如下:

public void addToDBO(List<List<EventElement>> insertList)
{
    SqlConnection s1 = new SqlConnection();
    s1.ConnectionString = filePath;
    s1.Open();
    SqlCommand command = new SqlCommand();
    command.Connection = s1;
    foreach (List<EventElement> item in insertList)
---------------------SNIP----------------------------
//Adding-data-to-database logic

我的意思是将加载的数据写入部署在localy中的数据库,但问题是它不接受我简单地将数据库的文件路径添加为filepath字符串的简化方法。

所以,总结一下: 如何正确连接到位于我的C:/?

上的数据库

提前谢谢。

1 个答案:

答案 0 :(得分:3)

enter image description here

您不只是传递文件路径。连接字符串需要遵循某种约定。例如

Provider=SQLNCLI10;Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;
Database=dbname;Trusted_Connection=Yes;

有关详情,请参阅http://www.connectionstrings.com/sql-server/