Solved

Query MS SQL with C#

Posted on 2009-04-08
6
1,277 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
DevOps Toolchain Recommendations

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

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 55
Replace &lt; with < 14 56
Server Error 11 47
Web Form VB.Net  import CSV 4 26
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

15 Experts available now in Live!

Get 1:1 Help Now