K
Ken Ellis
I am new to SQL and I am having trouble connecting to a database using
SQLServer Express.
I have created the database ok and have used VS 2005 to create a drag and
drop program
(no manual code) to access it, but when I try to access it with the code
below I get the message
"Cannot open database "MyDemo" requested by login. The login failed.
Login failed for 'Study\Ken' "
Its obviously to do with permissions but why should the generated program
work? I tried pasting the
connection string from the drag and drop config file into this program but
the errors I got made me realise
I was out of my depth.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private static string conString =
@"Data Source=(local)\SQLEXPRESS; Initial Catalog=KensDemo; Integrated
Security=True";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection testConnection = new SqlConnection(conString);
SqlCommand testCommand = testConnection.CreateCommand();
testCommand.CommandText = "Select demoValue from Demo where demoID = 1";
testConnection.Open();
string result = (string)testCommand.ExecuteScalar();
testConnection.Close();
MessageBox.Show(result);
}
}
SQLServer Express.
I have created the database ok and have used VS 2005 to create a drag and
drop program
(no manual code) to access it, but when I try to access it with the code
below I get the message
"Cannot open database "MyDemo" requested by login. The login failed.
Login failed for 'Study\Ken' "
Its obviously to do with permissions but why should the generated program
work? I tried pasting the
connection string from the drag and drop config file into this program but
the errors I got made me realise
I was out of my depth.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private static string conString =
@"Data Source=(local)\SQLEXPRESS; Initial Catalog=KensDemo; Integrated
Security=True";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection testConnection = new SqlConnection(conString);
SqlCommand testCommand = testConnection.CreateCommand();
testCommand.CommandText = "Select demoValue from Demo where demoID = 1";
testConnection.Open();
string result = (string)testCommand.ExecuteScalar();
testConnection.Close();
MessageBox.Show(result);
}
}