I've been experimenting with databases using the C# language. So far i have added, deleted, viewed, etc., my data all in one form. But now I am struggling to access and use it in other forms (e.g log-in window). How can I access my database information in all forms of my app? Can anyo开发者_StackOverflow中文版ne please help me.
If you are using web application then use state management (catching/Sesions) to solve your problem.
If you are using win apps then you can do one of the bellow
Create an class with db.cs and write methods which takes parms as sqlstirng and return the executed result as datatable/ dataset.( you can declare all the strings related to connect the db at class level strings)
you can pass the same data which you have retirved in the first page and pass it through the constructor of the next form.
I think 2nd point will solve your question.
For instance, you can set the connection string in the app.config section connectionStrings and then use it in any part of your code like this:
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
Then you can instantiate an new SqlCommand object to use to authenticate your users and so on...
精彩评论