Monday, March 26, 2012

How to connect to SQL Database, Create tables

Hi All, 1st of all happy New Year to all asp.net forum members
I am new at asp.net. I want to design a websiteusing asp.net as frontend and sql database as backend. I am able toconnect and add,update as well delete records when I use MsAcess andAsp.net using the following connection strings...
**********
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("/database/northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers where city LIKE 'Berlin' order by city ASC"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
**********

But when I try to connect to sql database using the following connection strings I am unable to do so...

**************
SqlConnection myConnection = new SqlConnection("server=PLATINUM\VSdotNET;database=pubs;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter(" * from Authors", myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");

MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();

**************
I have written the servername as "PLATINUM\VSdotNET" because when Iinstalled SQL SERVER 2000 I found a tray icon where the server name wasdisplaying the same (my computer name is PLATINUM).
When I used webmatrix I enterd the same server name and windowsauthentication I was able to create a database but How to createtable...

Please help me out
Thanks in advance...Please explain "I am unable to do so". Are you receiving an error message?|||Actually I don't know whether the connection string I am using is correct or not...
Because I am unable to create tables using the connection strigns what I told before...

when I try running this page I get the error message like
Compiler Error Message:CS1009: Unrecognized escape sequence
and the compiler shows a error in the connection string , So as far I guess there is problem with the connection string only.

If I get a sample page for what I am trying to do i.e connecting to SQLdatabase, Creating tables as well as accessing table my problem will besolved.
Thanks for your time.


|||Well, I always use this resource for building connection strings:http://www.connectionstrings.com/ Check out the SqlConnection (.NET) options under SQL Server. Yourproblem *might* be that you should use True, not Yes, forTrusted_Connection. In any case, I would try to stick as closelyas possible to the advice listed there.|||* from Authors is not a valid SQL Statement. Try SELECT * FROM Authors

No comments:

Post a Comment