-1

Hello please help me out with this connection string and even my procedure properties are attached

try
{
   conn = new SqlConnection("Server=RM-MOBL\MSSQLSERVER1;DataBase=master;Trusted_Connection=True;");

   conn.Open();
   SqlCommand cmd = new SqlCommand("dbo.new", conn);
   cmd.CommandType = CommandType.StoredProcedure;
   rdr = cmd.ExecuteReader();
   Console.WriteLine(" connection success");
}
// I hope I have mentioned correct connection string but 
// not able to execute my stored procedure

i am facing error

![please see error here ][3]

2 Answers2

0

The above code might not be a best way to Connect the database.

In your web.config add these lines which will connect to your database inside <configuration> section

<connectionStrings >
    <add name="myConnectionString" connectionString="Server=ServerName;Database=DatabaseName;User ID=UserId;Password=Password;Trusted_Connection=False;"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

And in your C# file add these references

using System.Data.SqlClient;
using System.Web.Configuration;

Inside Public Class add this Connection

 SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);

Now you can open the Connection by having con.Open();

Shreyas Achar
  • 1,407
  • 5
  • 36
  • 63
0

Try this,if you are using local machine

    SqlConnection con = new SqlConnection("Server=RANJITHM-OBL\MSSQLSERVER1;initial catalog=master;integrated security=true");
SqlCommand cmd = new SqlCommand("dbo.new", conn);
   cmd.CommandType = CommandType.StoredProcedure;
 if (con.State == ConnectionState.Closed)
                con.Open();
   rdr = cmd.ExecuteReader();
   Console.WriteLine(" connection success");
Akhil
  • 111
  • 1
  • 2
  • 8