For the last 3 hours I've been trying to figure out how ADO.NET works with no success. Could someone point me at a great tutorial or somethign similar? I am trying to build a DB from scratch and working with it in my WPF program.
I have worked before with JDBC and sqlite but I didn't find a tutorial from zero to a DB where I can connect and query.
Thanks for your help.
I still need one good example with building DB from zero. The Northwind example just doesn't work for me. Here's my code and the error I'm getting:
try
{
// step 1: create a SqlConnection object to connect to the
// SQL Server Northwind database
SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
// step 2: create a SqlCommand object
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
// step 3: set the CommandText property of the SqlCommand object to
// a SQL SELECT statement that retrieves a row from the Customers table
mySqlCommand.CommandText =
"SELECT CustomerID, CompanyName, ContactName, Address " +
"FROM Customers " +
"WHERE CustomerID = ‘ALFKI’";
// step 4: open the database connection using the
// Open() method of the SqlConnection object
mySqlConnection.Open();
//DEVELOPING YOUR FIRST ADO.NET PROGRAM 5
// step 5: create a SqlDataReader object and call the ExecuteReader()
// method of the SqlCommand object to run the SELECT statement
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
// step 6: read the row from the SqlDataReader object using
// the Read() method
mySqlDataReader.Read();
// step 7: display the column values
Console.WriteLine("mySqlDataReader[\"CustomerID\"] = " +
mySqlDataReader["CustomerID"]);
Console.WriteLine("mySqlDataReader[\"CompanyName\"] = " +
mySqlDataReader["CompanyName"]);
Console.WriteLine("mySqlDataReader[\"ContactName\"] = " +
mySqlDataReader["ContactName"]);
Console.WriteLine("mySqlDataReader[\"Address\"] = " +
mySqlDataReader["Address"]);
// step 8: close the SqlDataReader object using the Close() method
mySqlDataReader.Close();
// step 9: close the SqlConnection object using the Close() method
mySqlConnection.Close();
}
catch (SqlException e)
{
Console.WriteLine("A SqlException was thrown");
Console.WriteLine("Number = " + e.Number);
Console.WriteLine("Message = " + e.Message);
Console.WriteLine("StackTrace:\n" + e.StackTrace);
}
}
}
The error:
A SqlException was thrown
Number = 2
Message = A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)