Help with Sqlconnection manual code for book database
Dear Imar:
I am trying to write a test manual database connection in C# to the PlanetWrox database Genre table. I have some code that gets an error. The code is as follows:
-------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Data;
using System.Data.SqlClient;
public partial class Demos_dbconnect : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataReader rdr = null;
SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\BegASP NET\\Site\\App_Data\\PlanetWrox.mdf;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from Genre", conn);
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
// get the results of each column
int Id = (int)rdr["Id"];
string Name = (string)rdr["Name"];
int SortOrder = (int)rdr["SortOrder"];
Console.WriteLine(Id + " " + Name + " " + SortOrder);
Console.WriteLine();
}//
}//
}//
================================================== =======
The error is directed at the conn.Open(); line, which probably means that my manual connection string is incorrectly written. Could you please tell me how to make that code work? I am trying to write the manual code for the connection into the code behind file and make it work.
Thanks; Cullan
__________________
Cullan crothers
|