-
June 28th, 2012, 04:51 PM
#1
Connecting to SQL DB
I am new to programming and have installed C#2010 Express and SQL Express 2008. Via OSQL I have created a database, a table and added a record, I can do a select * and see the record etc.
I am trying to connect to this DB from C# with this:
Code:
SqlConnection con = new SqlConnection(@"Server=.\sqlexpress;Integrated Security=True;Connect Timeout=5;User Instance=True;database=JoJingles");
This is the error I am getting:
-
June 28th, 2012, 05:37 PM
#2
Re: Connecting to SQL DB
OK, I feel I am slowly getting somewhere. I now dont get an error so can only assume it is connecting, however I dont see any output on the console (Other than the "Program Running" one)
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace JoJinglesDB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection sqlConnection1 = new SqlConnection(@"Server=.\sqlexpress;Integrated Security=True;Connect Timeout=5;User Instance=True;database=JoJingles");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
System.Diagnostics.Debug.WriteLine("Program running");
cmd.CommandText = "SELECT * FROM Customer";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
System.Diagnostics.Debug.WriteLine("{0}\t{1}", reader.GetInt32(0),
reader.GetString(1));
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
}
}
-
June 28th, 2012, 05:38 PM
#3
Re: Connecting to SQL DB
I should add that the above is a copy from another site and not my own code!
-
July 1st, 2012, 01:08 AM
#4
Re: Connecting to SQL DB
Have you installed SQL Server Express on your laptop, and created a database called Jo Jingles?
-
July 1st, 2012, 10:16 AM
#5
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|