Hi all, having trouble with my first sql communication. I've got hosted service with an SQL database i've populated with a row.
When it gets to the third line the page crashes with an error.
SqlConnection connection = new SqlConnection("Server=mydbserver.com;Database=db198704784;");// +"Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT UserName FROM Users",connection);
SqlDataReader reader = cmd.ExecuteReader();
is there somewhere i need to put in my username or password? or is this code just wrong
Many thanks burnside.
-- Edited by longhorn2005
What is your error message?|||Try the below connection string
SqlConnection connection = new SqlConnection("Server=mydbserver.com;Database=db198704784;Integrated Security=True");
And if your database require Username and password check the below link
Building Connection String C#
HC
Thanks for the replys,
The page seems to crash when i uncomment out the third line of sql code "// SqlDataReader rdr = cmd.ExecuteReader();"
"System.Data.SqlClient" %> and i get this slightly unhelpful error message @.http://s152182516.websitehome.co.uk/test/default2.aspx
<%
@.PageLanguage="C#" %><%@.ImportNamespace="System.Data" %>
<%@.ImportNamespace="System.Data.SqlClient" %>
<!
DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
scriptrunat="server">protectedvoid Page_Load(object sender,EventArgs e){
// Define database connectionSqlConnection conn =newSqlConnection("uid=xxxxx;password=xxxxx;Server=mssql07.oneandone.co.uk;Database=db198704784;Integrated Security=True");SqlCommand cmd =newSqlCommand("select UserName from Users", conn);SqlDataReader rdr = cmd.ExecuteReader();
}
</
script><
htmlxmlns="http://www.w3.org/1999/xhtml"><
headrunat="server"><title>Untitled Page</title></
head><
body><formid="form1"runat="server"><div><asp:TextBoxID="TextBox1"runat="server"OnTextChanged="TextBox1_TextChanged"Height="176px"Width="338px"></asp:TextBox><asp:LabelID="Label1"runat="server"Text="Test Of SQL"></asp:Label></div></form></
body></
html>|||Try to open the connection before you call ExecuteReader() example below
SqlCommand cmd =newSqlCommand("select UserName from Users", conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
HC
--
Mark this post as ANSWERED if it helped you
|||I've tried the Open command but still no joy. I think i might have to hire someone to go into my MS hosting and check it and the SQL is setup, and make a little Logon aspx with the sql database so i can see the code to access it. If anyone is interested please email me.
|||Hello,
These are some tutorial for you:
http://www.codeproject.com/aspnet/SQLConnect.asp
http://samples.gotdotnet.com/quickstart/aspplus/doc/adoplusoverview.aspx
HTH
|||I tried to access the page you provided but unfortunately its unavailable. What do you mean by the page crashes? Can you post the complete error you are getting? Make sure the database exists and the table you trying to query exists in thedatabase.
Note: Never give out the username/password in the posts. replace them with XXXXX
Thanks
没有评论:
发表评论