TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
h dave
NA
14
1k
Not able to connect SQL databse
Mar 20 2020 4:49 PM
Hello,
Can anyone please help and give me suggestion or solution for one of the project
From the code mentioned below I am not able to connect databse thought I have tried to save the connection strin into app.config but nothing happnes
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.IO;
//using System.ServiceProcess;
//using Microsoft.SqlServer.Management.Common;
//using Microsoft.SqlServer.Management.Smo;
using System.Diagnostics;
using System.Threading;
using ENTITY;
using System.Configuration;
//<summary>
//Summary description for DBConnection
//</summary>
namespace OpenMiracle.DAL
{
public class DBConnection
{
public SqlConnection sqlcon;
string strServer = ".\\MSSQLSERVER";
public string path = string.Empty;
public DBConnection()
{
/* */
//-------------Single User----------------------//
strServer = ".\\MSSQLSERVER";
if (File.Exists(Application.StartupPath + "\\sys.txt"))
{
strServer = File.ReadAllText(Application.StartupPath + "\\sys.txt"); // getting ip of server
}
if (PublicVariables._decCurrentCompanyId > 0)
{
path = Application.StartupPath + "\\Data\\" + PublicVariables._decCurrentCompanyId + "\\DBOpenMiracle.mdf";
}
else if (PublicVariables._decCurrentCompanyId == 0)
{
path = Application.StartupPath + "\\Data\\DBOpenMiracle.mdf";
}
else
{
path = Application.StartupPath + "\\Data\\COMP\\DBOpenMiracle.mdf";
}
sqlcon = new SqlConnection(@"Data Source=" + strServer + ";AttachDbFilename=" + path + ";Integrated Security=True;Connect Timeout=120;User Instance=True");
//;Max Pool Size=200
/*
sqlcon = new SqlConnection(@"Data Source=192.168.2.112\SQLS2008;database=DBOpenMiracle;user id='open';password='miracle';Connect Timeout=30;");
//----------------Server----------------------//
*/
/// <summary>
/// SQL Express may take time to start up due to AutoClose Behaviour of SQLEXPRESS
/// Here we checking the sql connection and catches the logon error.
/// </summary>
try
{
if (sqlcon.State == ConnectionState.Closed)
{
sqlcon.Open();
}
}
catch (SqlException sEx)
{
int i = sEx.Number;
if (i == 233)
{
Reconnect();
}
else if (i == -1)
{
MessageBox.Show("OpenMiracle","SQL connection failure... Please check the instance name of your sqlexpress with " + Application.StartupPath + " \\sys.txt");
}
else if (i == 18493)
{
ChangeConnectionForServer();
}
else
{
MessageBox.Show("Could not connect to your database... Please check your SQL Configuration", "OpenMiracle");
}
}
catch (Exception)
{
//Catche any other exception
}
finally
{
}
try
{
PrintWorks.frmDBConnection.connectionString = sqlcon.ConnectionString;
}
catch
{
//sql Error
}
}
/// <summary>
/// SQL Express may take time to start up due to AutoClose Behaviour of SQLEXPRESS
///
/// </summary>
private void Reconnect()
{
try
{
Thread.Sleep(30000);
if (sqlcon.State == ConnectionState.Closed)
{
sqlcon.Open();
}
}
catch (Exception)
{
MessageBox.Show("Your SQL Server is starting up... Please close and re-open the application", "OpenMiracle");
}
}
/// <summary>
/// Cheanges the connection string to support SQLServer version instead of SQLExpress
/// </summary>
private void ChangeConnectionForServer()
{
sqlcon = new SqlConnection(@"Data Source=" + strServer + ";AttachDbFilename=" + path + ";Integrated Security=True;Connect Timeout=120;User Instance=True");
}
}
}
Reply
Answers (
1
)
Spacing in Listbox
amount increases by 50 after 15 days in c# windows applicat