place of a database file that can be use in ado.net
plz see thic code,i want to open(objConnection.Open();
) and close a database file in this codes,but i dont know where should i place the database file(in this codes:pubs)to work.plz help me
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();
}
}
If I understand you correctly, you are trying to open a SQL Server database but getting an error (let me know if that isn't the case). Under that assumption, I can say that your connection string is wrong. You have:
SqlConnection objConnection = new SqlConnection( "server=localhost;database=pubs;user id=sa;password=");
You need to replace the "database" keyword with a keyword called "Initial Catalog", as follows:
SqlConnection objConnection = new SqlConnection( "server=localhost;Initial Catalog=pubs;user id=sa;password=");
Let me know if this isn't what you're after.
精彩评论