How do you know what exactly to use as the database name?
In school the prof says he is not exactly a database programmer but gave us some code he had from an old one. That code worked fine if you want to actually connect to the database at school but I want to send everything right through sqlexpress on my own machine so I can work on the programs at home.
The following is the code to connect to the school database, how would I change it to connect through sqlexpress instead?
Code Snippet
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Database
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
SqlConnection connection;
public Class1()
{
}//closes constructor
public void connect(string computerID)
{
connection = new SqlConnection("Data Source = " + computerID +
"; Initial Catalog = test; Integrated Security = SSPI");
try
{
if(connection.State == ConnectionState.Closed)
connection.Open();
}//closes try
catch(Exception ex)
{
MessageBox.Show("connection no" + ex.Message.ToString());
}//closes catch
}//closes connection method
public void Write(string Name, string ID, string Grade, string Average)
{
string add = "INSERT INTO Example VALUES('" + Name + "', '" +
ID + "', '" + Grade + "', '" + Average + "');";
try
{
connect("DNV-WS-RM336INS");
SqlCommand command = new SqlCommand(add, connection);
command.ExecuteNonQuery();
}//closes try
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}//closes catch
this.connection.Close();
}//closes write mehtod
public string Read()
{
string Info = "";
SqlCommand command = new SqlCommand("select * from Example;", connection);
SqlDataReader reader = command.ExecuteReader();
try
{
while(reader.Read())
Info += reader.GetString(1).ToString() + " " +
reader.GetString(2).ToString() + " " +
reader.GetString(3).ToString() + " " +
reader.GetString(4).ToString() + "\n";
}//closes try
catch(Exception ex)
{
MessageBox.Show("read Method " + ex);
//Console.WriteLine("Error retrieving data.");
}//closes catch
reader.Close();
return Info;
}//closes read method
}//closes class
}//closes namespace
What I tried was replacing the dialog for the school database with just the text sqlexpress but that certainly didn't work.
And as a note the school uses visual C# 2003 but my machine actually uses visual studio 2005 and I have verified that sqlexpress is running on the machine.
|||I suggest that you might be better served by starting with something a bit more functional and useful. I would toss out the 'ol prof's' old unworking code, and start off with the tutorials built into Visual Studio. In MSDN Help, look up "Visual Basic Samples and Quickstarts"
Here are some additional resources to help you:
Configuration -Connect to SQL Express from "downlevel clients"
http://blogs.msdn.com/sqlexpress/archive/2004/07/23/192044.aspx
Configuration -Connect to SQL Express and ‘Stay Connected’
http://betav.com/blog/billva/2006/06/getting_and_staying_connected.html
K with a bit of research I jockied his code around a bit.
This is what I am using to send some test data currently, works perfect. Any thoughts on improving it though?
Code Snippet
public void DBWrite(string name)
{
// Create an empty SqlConnection object.
using (SqlConnection con = new SqlConnection())
{
// Configure the SqlConnection object's connection string.
con.ConnectionString = "Data Source=LAPTOP;Initial Catalog=Test2;Integrated Security=True";
// Open the database connection.
con.Open();
// Display information about the connection.
if (con.State == ConnectionState.Open)
{
string add = "INSERT INTO customer(CustomerID) VALUES('" + name + "');";
SqlCommand command = new SqlCommand(add,con);
command.ExecuteNonQuery();
}
else
{
MessageBox.Show("Not Open");
}
// At the end of the using block Dispose() calls Close().
}
}
public string DBRead()
{
string Info = "";
// Create an empty SqlConnection object.
using (SqlConnection con = new SqlConnection())
{
// Configure the SqlConnection object's connection string.
con.ConnectionString = "Data Source=LAPTOP;Initial Catalog=Test2;Integrated Security=True";
// Open the database connection.
con.Open();
SqlCommand command = new SqlCommand("select CustomerID from customer;", con);
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
Info += reader.GetString(0).ToString() + " " + "\n";;
}//closes try
catch (Exception ex)
{
MessageBox.Show("read Method " + ex);
//Console.WriteLine("Error retrieving data.");
}//closes catch
reader.Close();
}
return Info;
}//closes read method
|||Ah neat. With playing with it it looks as though when I leave out the datasource=LAPTOP or whatever else the servers I use will be it automatically makes the machine you are on the server, don't have to specify.
That makes it easier for me, don't have to change the datasource each time I switch from the pc to laptop and back again for development reasons.
Might seem like this was a no brainer but I have never done anything with databases before so it is pretty neat
No comments:
Post a Comment