Solved

Query MS SQL with C#

Posted on 2009-04-08
6
1,278 Views
Last Modified: 2013-12-17
I have added connection string to web.conf to query db with  C# to query /insert /updat to sql db. can any body help prviding me with working code that will query let's say select * from userTable. and display it on the webfile. I have set a website and havd .net framework 3.5 installed and using vs 2008.
<connectionStrings>
    <add key="ConnectionString" connectionString="Server=server; Database=db; uid=user; pwd=pass;" />
  </connectionStrings>

Open in new window

0
Comment
Question by:FD11
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
philipjonathan earned 125 total points
ID: 24103399
From your visual studio, create a web form (let's say Test1.aspx).
Then open the code-behind file (Test1.aspx.cs), and find the Page_Load method.
Then fill it in with this code.

Btw, don't forget to include the following namespaces, if they are not yet included:
using System.Configurations;
using System.Data;
using System.Data.SqlClient;

void Page_Load(object source, EventArgs e)
{
  string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlConnection connection = new SqlConnection(connectionString);
  connection.Open();
 
  string cmdText = "select * from userTable";
  SqlCommand cmd = new SqlCommand(cmdText, connection);
  SqlDataReader reader = cmd.ExecuteReader();
 
  while (reader.Read())
  {
    Response.Write(reader["column_name"].ToString() + "<br>");
  }
 
  reader.Close();
  connection.Close();
}

Open in new window

0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24103427
something like:

 string conn_str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            var myConnection = new SqlConnection(conn_str);
            myConnection.Open();
            var mySqlDataAdapter = new SqlDataAdapter("Select Top 10 * from Transactions", myConnection);

            var myDataSet = new DataSet();
            mySqlDataAdapter.Fill(myDataSet, "Transactions");
            foreach(DataRow myDataRow in myDataSet.Tables["Transactions"].Rows)
            {
                Response.Write(myDataRow["TransactionID"].ToString());
            }
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24103443
also try to change your connectionstring to
  <add name="ConnectionString" connectionString="Server=server; Database=db; uid=user; pwd=pass;" />
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 18

Expert Comment

by:philipjonathan
ID: 24103477
As sree_ven mentioned, take note that you change 'key' to 'name' in the web.config.
<add name="ConnectionString" connectionString="Server=server; Database=db; uid=user; pwd=pass;" />
0
 

Author Comment

by:FD11
ID: 24109192
That works, thank you guys
How would I take the data from the query and display each record in my formated table such as table that I design in the .aspx file. I have done that with php, but not with .net.  links to good websites,  tutorials ..etc would be good. thanks again
0
 
LVL 9

Assisted Solution

by:Sreedhar Vengala
Sreedhar Vengala earned 125 total points
ID: 24111029
You can straight away bind dataset to GridView Control  ex: http://www.codersource.net/asp_net_grid_view_whidbey.aspx
as good way to learn is with videos (free) from : http://www.asp.net/LEARN/videos/
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get the web content with different encodings 2 43
Finally almost got it 5 36
MediaHelp 4 21
DataTables + iCheck + pagination Issue 2 15
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now