|
A connection string is just a string that conforms to a particular pattern. The actual value that goes into the string depends on how the database is set up (e.g. is it accessed Windows Authentication) and other features such as whether or not you want Multiple Active RecordSets (MARS), which allows you to perform several reads using what appears to be the same connection. A good list of connection strings is available here[^]
It's traditional to store the connection string inside the .config file. There is a ConnectionStrings section (you won't see it in the default .config file, as you have to add it). Inside this ConnectionStrings section, you will add a ConnectionString entry that holds the details of the connection itself.
To retrieve the connection string in your code, you just need to use the following (here I'm assuming you've added your connection string into the config file with a key of MyConnectionString):
string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
|
|
|
|
|
I think the OP's question has got more to do with accessing a field in one class from another than with connection strings.
|
|
|
|
|
Whaddya know? I was right.
|
|
|
|
|
Pete O'Hanlon wrote: I was right.
No you're still not. He had used
SqlConnection con = new SqlConnection("Conn");
Removing the quotes around Conn would have worked since he had already declared Conn . The actual question has got to do with where he had declared the Conn variable and if his line of code had access to it (which is still not clear from the OP's post).
|
|
|
|
|
Funnily enough, by using the method I described it works for him without any cross class issues. So, what I told him to do was right as his code is now working.
|
|
|
|
|
hi friends i am not a boy ,i am a girl and actually i am very new to C# .In my company there is no one to teach that's why i had these kind of doubts. I searched so many websites but i didn't get proper thing that what i have searched.thanks friends ..
|
|
|
|
|
Member 9259606 wrote: hi friends i am not a boy ,i am a girl
Cool. Well, let me welcome you aboard Code Project (but with a username like that, it's so hard to guess what sex you are).
Don't worry about your posting. Quite often, you'll get answers that answer JUST the question you asked without considering what the alternatives are. In this case, you got both; it just happened that I thought you'd be better off using the in built .NET mechanism, rather than having to manage it yourself.
Anyway, I'm glad that you got the answer you wanted, and don't be afraid to post questions. That's what the forums are for.
|
|
|
|
|
This is the first time i am posting a question and in the first time itself i got negative points .So i was little bit scared to post questions again even though i posted and thanks Pete for appreciating me.
|
|
|
|
|
Welcome to CodeProject and don't worry about the downvoting. Please feel free to ask as many questions as you want, but remember to state your questions in a clear and easy-to-comprehend manner, preferably with an example and/or code.
|
|
|
|
|
Ignore the downvotes. They don't really count for anything, and don't be scared to post new questions. The more you ask, the more you learn how the forums work and you'll soon get used to it.
|
|
|
|
|
Firstly I would have a look here so that you can construct your connection string[^]
then I can only assume that you are after declaring a string variable that you can use through out your class, so then I would look at this
public class myDatabaseClass
{
private string _ConnectionString = "my database string here";
public void ShowString()
{
Debug.Print(_ConnectionString);
}
}
Myself personally I prefer to use the AppConfig to store such changeable information as its easier to change without having to recompile and distrubite the application again.
I would have a read of this as it will give you a start : Configuring a Connection String in the App.Config File During Runtime in C#[^]
I know it is for Visual Studio 2010 but it will give you the general idea.
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
you can use below code Separete class file
public SqlConnection con;
public SqlTransaction trans;
public SqlConnection CreateConnection()
{
//string strCon = "";
//strCon = System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString");
con = new SqlConnection(ConnectionString.Conn());
return con;
}
private void OpenConnection()
{
CreateConnection();
con.Open();
}
private void CloseConnection()
{
con.Close();
con.Dispose();
}
public void BeginTransaction()
{
OpenConnection();
trans = con.BeginTransaction(IsolationLevel.ReadUncommitted);
}
public void CommitTransaction()
{
trans.Commit();
//con.BeginTransaction();
trans.Dispose();
CloseConnection();
}
public void RollbackTransaction()
{
trans.Rollback();
trans.Dispose();
CloseConnection();
}
Call Bunn Layer
Connection conn = new Connection();
conn.BeginTransaction();
|
|
|
|
|
i want to create a connection string in class file and i have to use that connection string in my .cs file that is C#.net i m using visual studio 2008 and sqlserver 2005 .. anyone plz post ur ans r example for this
modified 13-Aug-12 9:17am.
|
|
|
|
|
This isn't going to go well for you.
The rules at the top say do not post programming questions here, and...
Member 9259606 wrote: anyone plz post ur ans r example for this
You will not get a response using language like that, use proper words and ask in the Quick Answers section.
Every man can tell how many goats or sheep he possesses, but not how many friends.
Shed Petition[ ^]
|
|
|
|
|
you just commited blasphemy. You posted a programming question in the lounge and violated about every rule in the book.
Please go to the C# forum and post your question there AFTER reading the guidelines (guidelines are always the first post in each forum, highlighted in yellow)
prepare for some downvoting and flaming, but don't take it personally, just follow the guidelines in the future and you'll be fine.
V.
modified 13-Aug-12 9:14am.
|
|
|
|
|
V. wrote: you just commited balphemy
Balphemist!
|
|
|
|
|
woopsy-daisy
V.
|
|
|
|
|
sure u can have answer, best 2 stare in space for long time - annswer will cum to u in flash
|
|
|
|
|
Yeah... annswer is a terrible guy he always make the same...
|
|
|
|
|
Norm .net wrote: best 2 stare in space for long time - annswer will cum to u in flash
That's what she said...
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Norm .net wrote: best 2 stare in space for long time - annswer will cum to u in flash
That's a completely different website.
|
|
|
|
|
Going to work I was listening a strange low music... you know that typical songs that are played during those old terror films just before the ghostly spirits make their entrance... I was wondering which kind of blasphemous horror had happened and then I saw your post...
|
|
|
|
|
Hi all,
I am developing a Winform appln in C#, which contains multiple tabs..
i want to use same shortcut keys on different tabs for different controls.
but i am not able to the same, can any one help ??
Thanks in advance..
|
|
|
|
|
Set the Form's KeyPreview property to true and handle the KeyDown/KeyPress events. Don't handle the KeyDown/KeyPress events of the tab or the constituent controls. Within the event handler, you can look for specific key combinations and take action based on the active tab.
|
|
|
|
|
I'd recommend a simple if or case statement (whichever you prefer) to identify the current selected tab.
switch (tabControl.SelectedIndex)
{
case 0:
break;
case 1:
break;
}
This would work under the assumption that you were using the tab indices to find out what's currently selected. There's also a function that allows you to check the current tab by its name. It'd work in the same respect as the .SelectedIndex function, expect you'd be using the tab names rather than the indices. It's the .SelectedTab function.
I hope this was of some help. Happy programming!
|
|
|
|