可以在ado.net中使用的数据库文件的位置

时间:2010-09-13 15:21:12

标签: c#

请参阅thic代码,我想打开(objConnection.Open();)并关闭此代码中的数据库文件,但我不知道我应该将数据库文件放在哪里(在此代码中:pubs)to work.plz帮助我

      public partial class Form1 : Form
      {
     SqlConnection objConnection = new SqlConnection(
 "server=localhost;database=pubs;" +
 "user id=sa;password=");
    SqlDataAdapter objDataAdapter = new SqlDataAdapter();
    DataSet objDataSet = new DataSet();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Set the SelectCommand properties... 
        objDataAdapter.SelectCommand = new SqlCommand();
        objDataAdapter.SelectCommand.Connection =
              objConnection;
        objDataAdapter.SelectCommand.CommandText =
         "SELECT au_lname, au_fname, title, price " +
         "FROM authors " +
        "JOIN titleauthor ON authors.au_id = " +
        "titleauthor.au_id " +
         "JOIN titles ON titleauthor.title_id = " +
        "titles.title_id " +
         "ORDER BY au_lname, au_fname";
        objDataAdapter.SelectCommand.CommandType =
             CommandType.Text;
        // Open the database connection... 
        objConnection.Open();
        // Fill the DataSet object with data... 
        objDataAdapter.Fill(objDataSet, "authors");
        // Close the database connection... 
        objConnection.Close(); 


    }
}

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您正在尝试打开SQL Server数据库但收到错误(如果不是这样,请告诉我)。根据这个假设,我可以说你的连接字符串是错误的。你有:

     SqlConnection objConnection = new SqlConnection( "server=localhost;database=pubs;user id=sa;password=");

您需要将“database”关键字替换为名为“Initial Catalog”的关键字,如下所示:

     SqlConnection objConnection = new SqlConnection( "server=localhost;Initial Catalog=pubs;user id=sa;password=");

如果这不是你想要的,请告诉我。