I have two questions, although they are very simple and basic...
1. I don't know what to fill in for the fields 'server', 'uid' and 'passwd'..
Is 'uid' and 'passwd' the same as the username/password you use for Windows?
What is the server? Is the 'server' always 'localhost' when the database is on the server?
Or if the server, uid, passwd are set, how to retrieve them?
Or if they are not set, how to set them?
2. Does somebody know a good tutorial for 'how to connect to database' (ASP.NET to MS SQL)? I tried a whole week working with tutorials, but I still not succeed (although I have experience with ASP and PHP/MySQL). Unfortunately, most tutorials are not giving that much information how to do it, for example the tutorial from ASP.NET. In that tutorial, the following sample (http://www.dotnetjunkies.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/pagelets/Pagelet6.src) is given:
MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PubsString"))
MyCommand = New SqlDataAdapter("select * from Titles where type='" & Category.SelectedItem.Value & "'", MyConnection)
I copied the code into a page (I work with ASP.Net Web Matrix), and run them (yes I also made a 'PubsString' database in MS SQL with the 'Titles' table etc), but too bad I got an ConnectionString error (The ConnectionString property has not been initialized.).. :(
I hope somebody can help me with my two problems... I would be really thankful!in a typical environment . IIS is on one server, and SQL is on another.
If they are on the same server, than IIS can talk to the SQL server as LocalHost.
If they are on different servers, than you need to know the server name.You also need a username/password combo for an account on the SQL machine. typically this will be ASPNET, the worker account used by IIS when it has to talk to other servers. Some Admin (you) would add this account to SQL logins and grant it appropriate access to the tables it needs.
The "PubsString" would be a connection string stored in the web.config of the website your building. In that file, you would have an entry like:
appSettings>
<add key="ConnectionString" value="Data Source=192.168.1.105;Network Library=DBMSSOCN;Initial Catalog=Pubs;Integrated Security=SSPI;" />
</appSettings
In this connection string, I Used the IP address of the SQL server instead of name.
Now, anywhere I want to use the SQLConnection object, i can pass the "PubsString"
as a connection string, hence MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PubsString"))
Search around for info on connection strings, and how to setup IIS/SQL to allow exchange of info (i.e. what accounts need to be where)
HTH
1. The "Server" is the server sql server is hosted on, you have to have this set. the uid/password refer to sql server logins, not windows logins. If you want to use windows logins, take out the uid/password fields in the connection string and put this in. Integrated Security=SSPI
2. I think your problem may be that you are trying to open your sqlserver connection, on a connection string in web.config, but you haven't set this in web.config. System.Configuration.ConfigurationSettings.AppSettings("PubsString") is more or less a variable in web.config. If you want this to work, add this in web.config.
<configuration>
<appSettings>
<add key="PubsString" value="SERVER=localhost;DATABASE=PubsString;Integrated Security=SSPI" />
</appSettings
<system.web>
...
</system.web>
</configuration>
and as far as the code you posted, start slow, do you know what DataAdapters are for, probably not, if you did you probably wouldn't use them as your first example...try this
Add a datagrid to your project, call it DataGrid1, then do this
Dim conn as new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PubsString"))
Dim cmd as new SqlCommand("select * from Titles where type=@.type", conn)
cmd.parameters.add("@.type", Category.SelectedItem.Value)
conn.open()
Dim rdr as Sqldatareader = cmd.ExecuteReader()datagrid1.datasource = rdr
datagrid1.databind()conn.close
Be warned I just typed all this code by hand, so if it works it would be a miracle, but if it doesn't it should be really close. If it works you should get a pretty datagrid with data in it.
Hope all this helps,
--Michael
0 comments:
Post a Comment