Test sql connect without using System.Data.SqlClient [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to test sql connection without using "System.Data.SqlClient"?
I want to check if the input ip, databasename, username and password of database is correct. I always used SqlConnection like this:
SqlConnection myConnection = new SqlConnection("network address=192.168.1.120; password=userpassword; user id=username; database=myDB");
But now I don't want to test sql server connection with this code.

You can use telnet to check the connectivity:
telnet yourhostorip 1433
And you can use sqlcmd to test both the authentication and the connectivity:
sqlcmd -S yourhostorip -U username -P password
Example how you can use it in C#:
var sqlProcess = new ProcessStartInfo();
sqlProcess.FileName = "SQLCMD.EXE";
sqlProcess.Arguments = String.Format("-S {0} -U {1} -P {2}",
server,
user,
password);
Process.Start(sqlProcess);

You can always use others
using System.Data.Odbc;
using System.Data.OleDb;

Related

Is there any alternative for netstat command in C#? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am writing a C# program which tells the Active Connections (Protocal, Local Address , Foreign Address , State , Process Id ) . To do this I am running cmd.exe as a process and passing netstat -ano as an argument.
System.Diagnostics.Process.Start("cmd.exe","netstat -ano");
This returns Active connections .
But I don't want to use this netstat command .
Is there any alternative for netstat.exe in C# ?Any library or something else so that I can get the same output ?
You do not need to run nmap or netstat, you can get all the info much easier when you take a look at the System.Net.NetworkInformation namespace. There you can find charming things like GetActiveTcpListeners() or GetActiveTcpConnections() and much more.
public static void ShowActiveTcpConnections()
{
Console.WriteLine("Active TCP Connections");
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation c in connections)
{
Console.WriteLine("{0} <==> {1}",
c.LocalEndPoint.ToString(),
c.RemoteEndPoint.ToString());
}
}
Details are here in the docs:
https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ipglobalproperties.getactivetcpconnections?view=net-5.0
I was looking for an netstat alternative as you and I found nmap.
To scan I usually run:
nmap -sP 192.168.1.*

Get data from remote sql server in a .NET web service [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have created a simple web service in visual studio 2015 with C#. I have set up an sql server database with a table and i want to simply return any value from that table.
By searching i haven't found a very explanatory tutorial.
Should i add a server connection in VS and then use linq or use SqlConnection?
I also included an ADO Entity data model in the project but i don't know how to use it in the webMethod.
Does any good tutorial exist about all options that i have to connect to the db and provide some examples, too?
Since it's a web application I suggest saving the connection string into web.config.
Example:
<connectionStrings>
<add name="ConnStringNameHere" connectionString="Data Source=IPadress;Initial Catalog=databaseName;Integrated Security=False;User ID=name;Password=pass" providerName="System.Data.SqlClient" />
</connectionStrings>
You just have to add the user in SQL Server with the appropriate permissions.
That way you can have a couple of connection strings, each for a user type with specific permissions (some can only read, some can write, etc.)
You make a connection to the database with this simple code:
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString))
using (SqlCommand cmd = new SqlCommand("SELECT * FROM user WHERE userName = username", connection))
{
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string id = (string)reader["Id"].ToString();
//work here
}
}
connection.Close();
}
And a useful link: http://www.codeproject.com/Articles/837599/Using-Csharp-to-connect-to-and-query-from-a-SQL-da

c# sql connection through internet [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was wondering if can I connect to sql database from another computer without installing sql server (is this possible). I am creating a c# wpf program that would get and insert data in sql database that is placed in my PC, in all computers where this program is installed using internet. Should I use in connection string my internet IP, or what, because I’m confused?
My connection string
String connString = #"Network Library=dbmssocn;
Network Address=127.0.0.1,1433;
Integrated security=SSPI;
Initial Catalog=db";
Also i forgot to say that i'm new in programing area, and i want to connect to sql database through lan connection
Assuming that your remote database (on your other computer) accepts remote connections, you can accomplish this via a traditional ADO.NET connection or you could use an ORM like Entity Framework to handle the connection.
You can see an example below that takes advantage of the SqlConnection class which does exactly what it's name implies :
using(var sqlConnection = new SqlConnection("Your Connection String"))
{
var query = "Your Query";
using(var sqlCommand = new SqlCommand(query,sqlConnection))
{
sqlConnection.Open();
// Execute your query here using sqlCommand.ExecuteNonQuery()
// or some other execution method
}
}
You would just need to ensure that your actual connection string to target your remote database is correct.
Yes, it is possible. You could use ADO.NET or Entity Framework to accomplish this goal. However, I would recommend setting up a web api on the sever you have hosting the database, which you could call to get/post data. I personally wouldn't hard code any connection strings in the program you plan to deploy to end user workstations. Just call the web api and let your web api talk to the database.

Connecting Database using c# in Visual Studio 2013 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a problem connecting my database (created in SQL Server 2008 R2 Express) with c# in vs 2013
Here is the code I wrote
string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ToString();
SqlConnection conn = new SqlConnection(connStr);
the error is NullReferenceException
Change this:
string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ToString();
to...
string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ConnectionString;
Cheers -
Where did you define your connection string? In the app.config? Please send us the place where you configure this.
See http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx for help.
See http://www.connectionstrings.com/ how to configure a connection string for .net
I assume that there is no connection string named newSchool. It is probably NewSchool or some other casing.
In response to your comment: You need to provide a proper connection string. The ConfigurationManager is used to get a value from the configuration. The NullReferenceException shows you that the setting you request is not there.
You need to create a connection string first using the settings manager and then you can access it.

adding computer name to active directory group [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a requirement where I have to add computer name to the pre-defined active directory group. Does anyone have experience related to this ?
You can do this through C# code using System.DirectoryServices
// Bind to the Computers container and add a new computer.
DirectoryEntry de01 = new DirectoryEntry("LDAP://CN=Computers,DC=fabrikam,DC=com");
DirectoryEntry newComputer = de01.Children.Add("CN=New Computer", "computer");
newGroup.CommitChanges();
See http://msdn.microsoft.com/en-us/library/ms180832(v=VS.90).aspx for examples.
Use ADSI. Basically you bind to the Active Directory, locate the computer object and group object you need, and call a method to add the computer to the group.
If it needs to be from a command line, you can easily do this using WSH or .Net.
ADSI reference:
http://msdn.microsoft.com/en-us/library/aa772218(v=VS.85).aspx
ADSI LDAP:
http://msdn.microsoft.com/en-us/library/aa746384(v=VS.85).aspx
It will not take effect until the computer reboots.
Below code worked for me
//Step 1: get the DN of the adgroup you want to use
DirectoryEntry groupEntry = new DirectoryEntry ("LDAP://CN=AdgroupNameWewantToAddTo,DC=na,DC=ABCint,DC=com");//adgroupDSN
//step 2: get the Dn of the pc you want to add to the group
string memberDN;
DirectorySearcher dom = new DirectorySearcher(baseDN);
dom.Filter = "(&(ObjectCategory=computer) (cn=" + PCNAME+ "))";
SearchResult searchResult = dom.FindOne();
if (searchResult != null)
{
memberDN=searchResult.GetDirectoryEntry().Path;
}
//Step 3:add PC to the ad group
groupEntry.Invoke("Add", computerdn);

Categories