Problem in connecting SQL 2005 to Visual Studio 2008

Closed
Shimshim Posts 4 Registration date Tuesday October 16, 2012 Status Member Last seen October 17, 2012 - Oct 16, 2012 at 07:10 PM
Shimshim Posts 4 Registration date Tuesday October 16, 2012 Status Member Last seen October 17, 2012 - Oct 17, 2012 at 08:03 PM
Hello,

I'm creating a system that can save the employee record, I named it table EMPLOYEE. I have installed visual studio 2008 and sql server 2005 in my computer. This is the sample code with the connection string:
try
{
SqlConnection connect = new SqlConnection();

connect.ConnectionString = @"server = PC-NAME/SQLEXPRESS;data source = localhost; initial catalog = myDatabasename; integrated security = SSPI";
connect.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO EMPLOYEE(EMP_NO,EMP_LAST,EMP_FIRST,EMP_POS,EMP_OFC,EMP_DIV,EMP_GEN)" + "VALUES('" + emp_num.Text + "'" + "," + "'" + emp_last_name.Text + "'" + "," + "'" + emp_first_name.Text + "'" + "," + "'" + emp_pos.Text + "'" + "," + "'" + emp_ofc.Text + "'" + "," + "'" + emp_div.Text + "'" + "," + "'" + emp_gen.Text + "'" + ")", connect);
//SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd, connect);
cmd.ExecuteNonQuery();
connect.Close();
MessageBox.Show("Record has been saved.");
}
catch (Exception ex)
{
MessageBox.Show(ex.GetBaseException().ToString(), "Connection Status");

}

I also tried some tips from the internet, like:
- made the SQL Server Browser service Automatic.
- enabled the Protocols for SQLEXPRESS TCP/IP.

Still, I cannot connect to database.
The connection status states:

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

Can someone help me? Thanks.
Related:

2 responses

Shimshim Posts 4 Registration date Tuesday October 16, 2012 Status Member Last seen October 17, 2012
Oct 17, 2012 at 07:47 PM
I have tried to create a .udl file. I selected microsoft OLE DB Provider for SQL Server in the provider tab. I got an error message in connection tab when I clicked the Test Connection button. I entered the server name NAME-PC/SQLEXPRESS. I chose the radiobutton Use Windows NT Integrated security. Then, I chose the radiobutton Attach a database on the server. My database name was not included in the dropdown list so I typed it on my own. I searched for the location of my database name in the Using the filename textbox.

The Microsoft Data Link Error said:
Test Connection failed because of an error in initializing provider. Directory lookup for the file "C:\Users\mydatabasename.sdf" failed with the operating system error 5(Access is denied).

can someone explain to me the connection of server to VS in step-by-step procedure?

My O.S is Windows7 Ultimate.
I installed Microsoft Visual C# 2008 Express Edition and Microsoft SQL Server 2005 Express Edition.

please help me. i'm dying here :(
0
Shimshim Posts 4 Registration date Tuesday October 16, 2012 Status Member Last seen October 17, 2012
Oct 17, 2012 at 07:58 PM
I think the problem is whenever I enter the database name in the textbox 'Select the database on the server' because my database name was not in the drop-down list. So the 'Test Connection' button failed.
0
Shimshim Posts 4 Registration date Tuesday October 16, 2012 Status Member Last seen October 17, 2012
Oct 17, 2012 at 08:03 PM
please help me in the connection. i've been doing this for 2 weeks!!!!
0