?
Solved

connect to a sql server Db with c#

Posted on 2011-05-02
7
Medium Priority
?
321 Views
Last Modified: 2012-05-11
Am using the below code to connect to the sqlsever db have not set any passwords or usernames in the dB and am not sure what is supposed to be set or not set to access the db via the C# code any help woudl be appreciated am using visual studio 2005 and sqlserver 2005 when you run the code connection1.Open();
 it just comes back cannot connect
  private void connectsqlserver_Click(object sender, EventArgs e)
        {

            SqlConnection connection1 = new SqlConnection("user id='';" +
                                       "password='';server=serverurl;" +
                                       "Trusted_Connection=yes;" +
                                       "database=Extract; " +
                                       "connection timeout=30");
           
            connection1.Open();
            SqlCommand insertCommand = new SqlCommand(
                "INSERT INTO tblMainRecInfo (journaltitle) VALUES ('datalkjlkjk')",connection1);
            insertCommand.ExecuteNonQuery();
            connection1.Close();
           
           }
0
Comment
Question by:sydneyguy
  • 3
  • 2
  • 2
7 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 400 total points
ID: 35504299
connection string like this

  <add name="MyDbConn1"
       connectionString="Server=MyServer;Database=MyDb;Trusted_Connection=Yes;"/>


refr this
http://msdn.microsoft.com/en-us/library/ff647396.aspx
0
 

Author Comment

by:sydneyguy
ID: 35504311
is this for c# ?? connection
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35504324
its in web.config of C# project get more details for that link

in code you can use like this

 SqlConnection connection1 = new SqlConnection("Server=MyServer;Database=MyDb;Trusted_Connection=Yes;");
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 33

Expert Comment

by:hongjun
ID: 35504335
Try this

SqlConnection connection1 = new SqlConnection("Data Source=serverurl;Initial Catalog=Extract;Integrated Security=SSPI;");

Open in new window


Please replace the serverurl with the server name.
0
 

Author Comment

by:sydneyguy
ID: 35504383
SqlConnection connection1 = new SqlConnection("Data Source=DICKSMITHMIR-PC\SQLEXPRESS;Initial Catalog=PubMedExtract;Integrated Security=SSPI;");

this is the connection string but it does not like the DICKSMITHMIR-PC\SQLEXPRESS expecially the \ mark in the equation
0
 
LVL 33

Accepted Solution

by:
hongjun earned 1600 total points
ID: 35504391
try this

 
SqlConnection connection1 = new SqlConnection(@"Data Source=DICKSMITHMIR-PC\SQLEXPRESS;Initial Catalog=PubMedExtract;Integrated Security=SSPI;");

Open in new window

0
 

Author Closing Comment

by:sydneyguy
ID: 35504424
that got the local going but will need to lift this up to the sever eventually so thanks for the page link
garry
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are like me and like multiple layers of protection, read on!
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question