Program to test a SQL connection string? [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I need a free and quick to download program that can test a connection string.

You can make one yourself in 20sec. For example in C#
- Create a new WinForms application
- Create a new SqlConnection(connectionString)
- Exception => Bad connection string
- All ok => Good connection string
SqlConnection conn = null;
try {
conn = new SqlConnection("connection string here");
conn.Open();
// Good connection string
} catch (SqlException sqlE) {
// Bad connection string
} finally {
if (conn != null) conn.Dispose();
}

An abbreviated version of Xyphrax's answer (assuming you're running this in the debugger):
using(var conn = new SqlConnection("Connection String Here"))
conn.Open();

You can use a tinny Windows application tool: http://www.webpowersoftware.com/App/Tools/SqlServer/SqlServerConnectionTest.exe.

Related

LogIn form, SQL exception

I'm trying to make a simple program that has a log-in part, with a local database just for testing.And i keep getting an error when I try to open the connection to the SQL database.
private void logInButton_Click(object sender, EventArgs e)
{
MainMenu openMainMenu = new MainMenu();
SqlConnection sqlcon = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\Nea Florin\Desktop\PlatformaTestare\PlatformaTestare\Server.mdf;Integrated Security=True;Connect Timeout=30");
sqlcon.Open();
SqlCommand cmd = new SqlCommand("Select * from Table Where username ='" + usernameTextBox.Text + "' and password = '" + passwrodTextBox.Text + "'");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
if (dtbl.Rows.Count > 0)
{
openMainMenu.Show();
this.Hide();
}
else
MessageBox.Show("Wrong username or password!");
}
I get the error at sqlcon.Open();, and it is: "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: An attempt to attach an auto-named database for file C: \Users\Nea Florin\Desktop\PlatformaTestare\PlatformaTestare\Server.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
Well, the best advice I can give you is to google the error message. Keep in mind that if there is an error message it means that the problem is well known an as such it's a safe bet that someone have encountered it before you and managed to solve it. The first 4 results of this search are on stackoverflow and at least two of them have accepted answers, so I believe a little reasearch would have saved you a long time.
This is the best advice because it streaches far beyond your current problem. I firmly believe that good searching skills is the most important and most powerfull tools of a sotfware developer. I can assure you, no matter how much time you are developing software, almost every exception you get, someone else have already solved and posted the solution somewhere, you only need to find it.
Now, as for the code it self - You have some major problems other then the exception you are asking about:
Concatenating strings into sql statements instead of using parameters expose your code to SQL injection attacks. This is a very serious threat that is extremely easy to fix.
Using insntances of classes that implements the IDisposable interface without properly disposing them may lead to memory leak. Read about the using statement and make it a habit to use it every time it's possible.
Exception handling. Currently, if your database can't be reached, you get an exception and your program crash. You should use a try...catch block anywhere you can't control in code to let your program end gracefuly instead. (Don't ever use try...catch for things you can do in code such as validate user input or checking division by zero - only for things that are beyon your control such as database availability.)
Having said all that, your code should look something like this:
private void logInButton_Click(object sender, EventArgs e)
{
using (var sqlcon = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|C:\Users\Nea Florin\Desktop\PlatformaTestare\PlatformaTestare\Server.mdf;Integrated Security=True;Connect Timeout=30"))
{
sqlcon.Open();
using (var cmd = new SqlCommand("Select 1 from Table Where username = #userName and password = #password"))
{
cmd.Parameters.Add("#userName", SqlDbType.NVarChar).Value = usernameTextBox.Text;
cmd.Parameters.Add("#password", SqlDbType.NVarChar).Value = passwrodTextBox.Text;
using (var dtbl = new DataTable())
{
using (var sda = new SqlDataAdapter(cmd))
{
sda.Fill(dtbl);
}
if (dtbl.Rows.Count > 0)
{
var openMainMenu = new MainMenu();
openMainMenu.Show();
this.Hide();
}
}
else
{
MessageBox.Show("Wrong username or password!");
}
}
}

how to pass error class object as a result in API? [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 4 years ago.
Improve this question
I have create One API controller and i want to pass custom error class object as a response.
Can any one help me for that ?
Thanks in advance.
Create one class for to store error information as shown below.
ErrorInformation objError = new ErrorInformation();
try
{
test(ref Token, ref SessionToken);
}
catch (Exception ex)
{
objError.ErrorMessage = ex.Message;
objError.StatusCode = Convert.ToInt16(HttpStatusCode.ExpectationFailed);
objError.ErrorType = ex.GetType().ToString();
objError.ErrorCode = "E01";
throw new HttpResponseException(Request.CreateResponse<ErrorInformation>(HttpStatusCode.ExpectationFailed, objError));
}
This will return whole object as result.
{
"StatusCode": 417,
"ErrorMessage": "Error Detail",
"ErrorType": "System.DivideByZeroException",
"ErrorCode": "E01"
}

Can anybody tell the code to import only selected data(rows and columns) from Excel to SQL through asp.net? [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 7 years ago.
Improve this question
Like here if I want to import from row 5 and column C
protected void Button1_Click(object sender, EventArgs e)
{
String csvPath = Path.Combine( Server.MapPath("~/Files/") +
Path.GetFileName(FileUpload1.PostedFile.FileName));
FileUpload1.SaveAs(csvPath);
DataTable dt = new DataTable();
dt.Columns.AddRange(
new DataColumn[3]{
new DataColumn("KPI", typeof(string)),
new DataColumn("KPIPN", typeof(string)),
new DataColumn("KPIPV", typeof(string))
});
string csvData = File.ReadAllText(csvPath);
You can use the LinqToExcel project
https://github.com/paulyoder/LinqToExcel

Plagiarism Checker C# based API [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 7 years ago.
Improve this question
I am looking for a plagiarism checker API that is based on C# code. I need to use it on my web service. I need to easily query plagiarism checking engine and get result for the originality of the text.
If you know any service that is similar to what I am asking it would be great!
I’m using an online plagiarism checker service called Copyleaks which offers an interface to integrate with their API (HTTP REST). It also provides interface which is fully compatible with C#.
Steps to integrate with Copyleaks API:
Register on Copyleaks website.
Create a new C# Console application project and install Copyleaks’ Nuget Package.
Use the following code which performs a webpage scan.
This code was taken from its SDK (GitHub):
public void Scan(string username, string apiKey, string url)
{
// Login to Copyleaks server.
Console.Write("User login... ");
LoginToken token = UsersAuthentication.Login(username, apiKey);
Console.WriteLine("\t\t\tSuccess!");
// Create a new process on server.
Console.Write("Submiting new request... ");
Detector detector = new Detector(token);
ScannerProcess process = detector.CreateProcess(url);
Console.WriteLine("\tSuccess!");
// Waiting to process to be finished.
Console.Write("Waiting for completion... ");
while (!process.IsCompleted())
Thread.Sleep(1000);
Console.WriteLine("\tSuccess!");
// Getting results.
Console.Write("Getting results... ");
var results = process.GetResults();
if (results.Length == 0)
{
Console.WriteLine("\tNo results.");
}
else
{
for (int i = 0; i < results.Length; ++i)
{
Console.WriteLine();
Console.WriteLine("Result {0}:", i + 1);
Console.WriteLine("Domain: {0}", results[i].Domain);
Console.WriteLine("Url: {0}", results[i].URL);
Console.WriteLine("Precents: {0}", results[i].Precents);
Console.WriteLine("CopiedWords: {0}", results[i].NumberOfCopiedWords);
}
}
}
Call the function with your Username, API-Key and the URL of the content you wish to scan for plagiarism.
You can read more about its server in "How To" tutorial.

Hydbrid App In IBM Worklight [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 8 years ago.
Improve this question
I Already done updating the password in database using SQLAdpaters in IBM Worklight Hybrid App.
I am Working on Hybrid App using IBM Worklight. I am updating user password in database using SQLAdapter, but I want to store password in encrypted format. I already have the encryption and decryption logic in java class. How can I integrate that java class with my hybrid app?
var procedure1Statement = WL.Server.createSQLStatement("UPDATE USERS SET USERPASSWORD=? WHERE USERNAME = ? AND USERPASSWORD=? ");
function updateUserPassword(newPassword,userName,password) {
return WL.Server.invokeSQLStatement({
preparedStatement : procedure1Statement,
parameters : [newPassword,userName,password]
});
}
changed code as follows
var userpwdUpdateStatement = WL.Server.createSQLStatement("UPDATE USERS SET USERPASSWORD=? WHERE USERNAME = ? AND USERPASSWORD=? ");
function updateUserPassword(newPassword,userName,password) {
var encryptdecryptutility = new com.abcd.bgf.SysCRAESencrpDecrp();
var encryptnewPassword = encryptdecryptutility.encrypt(newPassword);
var encryptoldPassword = encryptdecryptutility.encrypt(password);
return WL.Server.invokeSQLStatement({
preparedStatement : userpwdUpdateStatement,
parameters : [encryptnewPassword,userName,encryptoldPassword]
});
}