Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am trying to run script file using

C#
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;


but iam getting error in connection with server
Error:

Quote:
{"Failed to connect to server Data Source=(local);Initial Catalog=DatabaseName;User ID=sa; Password=123123;."}

my code is
FileInfo file = new FileInfo(filePath);
string script = file.OpenText().ReadToEnd();
string conn = "Data Source=.;Initial Catalog=edustarsDBwithFeesPayments;User ID=sa; password=123123;";
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);


i also tried many types of connection string but still getting same error.




Quote:
Exception Details: System.ComponentModel.Win32Exception: The network path was not found
Posted
Comments
Pranav-BiTwiser 1-Apr-14 9:57am    
check the services are on or not....

Quote:
i also tried many types of connection string


No it won't help.Give the correct path

Look at this:
string conn = "Data Source=.; Initial Catalog=edustarsDBwithFeesPayments;User ID=sa; password=123123;";

DataSource= . ? :doh:

Give correct server address to datasource.Likewise,Database,username,password everything should be correct
and please see for more details:
https://www.connectionstrings.com/[^]
 
Share this answer
 
v3
Comments
abdussalam143 1-Apr-14 9:25am    
i also tried these two
string conn = "Data Source=192.168.0.14; Initial Catalog=edustarsDBwithFeesPayments;User ID=sa; password=123123;"
and
string conn = "Data Source=192.168.0.14,1433; Initial Catalog=edustarsDBwithFeesPayments;User ID=sa; password=123123;"
Tom Marvolo Riddle 1-Apr-14 9:28am    
Click management studio copy the server name and paste it in datasource.just try it
Tom Marvolo Riddle 1-Apr-14 9:28am    
Try it & let me know
abdussalam143 2-Apr-14 1:46am    
i use this to connect
Data Source=ABDUSSALAM;Initial Catalog=edustarsDBwithFeesPayments;User ID=sa; Password=123123;

ABDUSSALAM is my server name

Failed to connect to server Data Source=ABDUSSALAM;Initial Catalog=edustarsDBwithFeesPayments;User ID=sa; Password=123123;
abdussalam143 2-Apr-14 7:00am    
thanks for your help buddy
FileInfo file = new FileInfo(filePath);
string script = file.OpenText().ReadToEnd();
string ServerName= "ABDUSSALAM";
Server server = new Server(ServerName);
server.ConnectionContext.ExecuteNonQuery(script);





new Server(ServerName);
in this constructor we can give two type of parameters one is connection string and one is server name

I replace connection string with Server Name ans it done the trik.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900