Solved

Query MS SQL with C#

Posted on 2009-04-08
6
1,276 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

20 Experts available now in Live!

Get 1:1 Help Now