Solved

Query MS SQL with C#

Posted on 2009-04-08
6
1,280 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 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