Showing posts with label wrong. Show all posts
Showing posts with label wrong. Show all posts

Tuesday, March 27, 2012

attach db

How can I attach a remote db via Enterprise Manager or Query Analyzer?
Am I wrong if I launch a "sp_attach" sql file (from my local machine) to the remote server?
Do I really have to detach local db before?
Please can someone just tell me how he did?

SaoriHi saori,
1. Did you try to post your question in the DBA section of these forums:

http://www.sqlteam.com/Forums/


http://www.sql-server-performance.com/forum/default.asp

2. Hope some DBA will help you.|||Thanks for the answer.
I'll (quickly) give it a try.

(I posted a msg there 'cause it is above all a question of method about sql server tools)

Saori|||Honestly, I attach remote db's by creating a new registration. Unless you mean something else like fiberchanneling or something, but as for having your SQL Enterprise Manager utilize a Remote SQL Server, just create a new registration.

For T-SQL based methods, you're not necessarily attaching. I'd say it's more Linked Server, or Using Remote Servers. check books online for those two in the Index tab.

SP_LinkedServers and the other method would be actually using the enterprise manager to create the registration.|||Saori,

You didn't clearly define your question about what you want to 'attach', but I think I have understood what you were saying. I would redefine your question with several possible solutions.

Assume, you have a local SQL server in your machine, and there is another SQL server in a remote machine. There is a database called pubs1 existing in the remote SQL server. You want it APPEARS ('attach') in your SQL server.

1. Logical APPEAR:
As KraGiE suggested you could make a new Server Registration in your local SQL Enterprise Manager to LINK to that remote SQL server. In this way, you would bring the whole SQL server including the pubs1 into your local SQL Enterprise Manager.

However, to do so, you need to have the access to the database, say: machine name, login name and password. It is possible that you could only access one database in that server, say, pubs1. But that is enough for you. By the way, if the remote server is not within your LAN, and need to be linked through HTTP, you should have the server IP address and also the access to possible firewall.

I called this kind of 'attach' as logical APPEARS, because the database is not physically in your machine, but still at the remote server. You can only access the database through your Enterprise Manager. The database pubs1 appearing in your Enterprise Manager is just a pointer linked to the remote server.

2. Physical APPEAR:
In short, get a physical copy from the remote server and 'attach' it to your local server.

Step 1:
Copy the database file and the log file into your local machine. In this case, pubs1 files are located by default at:
C:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs1.MDF
C:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs1_log.LDF
You need shut down the remote database for copy/past, otherwise, it wouldn't work.

Step 2:
from Enterprise Manager, right click the Databases object in your local SQL instance, choose all tasks --> attach database ... . From the Attach Database window, choose your database file, pubs1, and then OK.

3. If you didn't have SQL 2000, instead of SQL 7.0, then the Step 2 would not avaiable in Enterprise Manager. You would have to use the command 'sp_attach_db' to do the job. Something like:


EXEC sp_attach_db @.dbname = N'pubs',
@.filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs1.mdf',
@.filename2 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_log1.ldf'

4. Finally, if you even didn't have SQL server 7.0, you couldn't do the trick of 'attach'. But, you could do the job with SQL server 6.5 Enterprise Manager 'Database/Object transfer' tool given you have access to the remote database, where there is equivalent tools in both SQL 7.0/2000, called Import Data/Export Data, but not as easy used as the ATTACH method.

Hope this helps.|||Thanks Phuoc, KraGiE and ghan.
This really helped me understand essential issues.

Great analysis , ghan (copied in my notepad)!
Just one little thing:
When I attach db via Enterprise Manager, do I automatically transfer log file as well?|||saori,

I am really happy to hear that. You know what, when I made some analyses to other people, I just summarize what I have done and clear my understanding about this issue myself.

When I attach db via Enterprise Manager, do I automatically transfer log file as well?

You see the example:


EXEC sp_attach_db @.dbname = N'pubs',
@.filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs1.mdf',
@.filename2 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_log1.ldf'

There are two files, one is *.mdf, the data file, another is *.ldf, the log file.

When you use enterprise manager to do the job, you will see, (at STEP 2) you only need browse the *.mdf file in, while the *.ldf file will be brought in automatically, assuming you have copied the two files into the same directory (Step 1).

Good luck.|||I have a project lets say project1. I have a copy of it running on my development server and a copy of it on my production server. I have detached db from development server and attached it to my production server. I can work in db using Enterprise manager but when I try to run my applications it doesnt work. I know why it doesnt work its because its looking for user developmentserver and now its on productionserver. Is there a way to set permissions for user productionserver without having to do every table and stored procedure one at a time? I would like to do it quickly and efficiently and doing all by hand one at a time is neither.
Please help.|||There is a security issue. You must use Windows Authentification, instead of SQL Server Authentification. You might change your connection string to use SQL Server authentification. If you want to keep to use windows authentification, you need to setup your security in your production server to make your application assistent with your SQL server.

Sunday, March 25, 2012

Attach a DB that was never Detached

Sorry if this is the wrong place to ask this. I have a file "restored.mdf",
that was simply copied from the disk by backup software, i need the data
inside that file. It is 12GB. the file was backed up from
E:\SQLData\MSSQL\Data\ABC.mdf, and is now in
E:\SQLData\MSSQL\Data\restore\restored.mdf.
Is there any way to attach this database to any SQL Server 2000 Server ? If
so, i can then just copy the data from one table to another. I tried
EXEC sp_attach_single_file_db @.dbname = N'restored',
@.physname = N'E:\SQLdata\MSSQL\Data\restore\restored.mdf'
But when i run this i get:
Server: Msg 5105, Level 16, State 4, Line 1
Device activation error. The physical file name
'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
Server: Msg 1813, Level 16, State 1, Line 1
Could not open new database 'restore_AVC'. CREATE DATABASE is aborted.
Device activation error. The physical file name
'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
It looks like it is looking for the original logfile, i guess because this
database was never actually detached.
Attaching databases that were not detached properly is unreliable, and does
not always work.
If there were open transactions, you will need to use sp_attach_db instead
(as these transactions will need to be rolled forward when the database is
attached). You will therefore need access to the log.
John.
hogwart wrote:
> Sorry if this is the wrong place to ask this. I have a file
> "restored.mdf", that was simply copied from the disk by backup
> software, i need the data inside that file. It is 12GB. the file
> was backed up from E:\SQLData\MSSQL\Data\ABC.mdf, and is now in
> E:\SQLData\MSSQL\Data\restore\restored.mdf.
> Is there any way to attach this database to any SQL Server 2000
> Server ? If so, i can then just copy the data from one table to
> another. I tried
> EXEC sp_attach_single_file_db @.dbname = N'restored',
> @.physname = N'E:\SQLdata\MSSQL\Data\restore\restored.mdf'
> But when i run this i get:
> Server: Msg 5105, Level 16, State 4, Line 1
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> Server: Msg 1813, Level 16, State 1, Line 1
> Could not open new database 'restore_AVC'. CREATE DATABASE is aborted.
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> It looks like it is looking for the original logfile, i guess because
> this database was never actually detached.
|||Give the suggestion here a try (no guarantees). Make a backup of your files
first though.
http://tinyurl.com/3s4b6
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backups? Use MiniSQLBackup Lite, free!
"hogwart" <hogwart@.dannyland.com> wrote in message
news:%230tcOwzlEHA.3156@.TK2MSFTNGP12.phx.gbl...
> Sorry if this is the wrong place to ask this. I have a file
"restored.mdf",
> that was simply copied from the disk by backup software, i need the data
> inside that file. It is 12GB. the file was backed up from
> E:\SQLData\MSSQL\Data\ABC.mdf, and is now in
> E:\SQLData\MSSQL\Data\restore\restored.mdf.
> Is there any way to attach this database to any SQL Server 2000 Server ?
If
> so, i can then just copy the data from one table to another. I tried
> EXEC sp_attach_single_file_db @.dbname = N'restored',
> @.physname = N'E:\SQLdata\MSSQL\Data\restore\restored.mdf'
> But when i run this i get:
> Server: Msg 5105, Level 16, State 4, Line 1
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> Server: Msg 1813, Level 16, State 1, Line 1
> Could not open new database 'restore_AVC'. CREATE DATABASE is aborted.
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> It looks like it is looking for the original logfile, i guess because this
> database was never actually detached.
>

Attach a DB that was never Detached

Sorry if this is the wrong place to ask this. I have a file "restored.mdf",
that was simply copied from the disk by backup software, i need the data
inside that file. It is 12GB. the file was backed up from
E:\SQLData\MSSQL\Data\ABC.mdf, and is now in
E:\SQLData\MSSQL\Data\restore\restored.mdf.
Is there any way to attach this database to any SQL Server 2000 Server ? If
so, i can then just copy the data from one table to another. I tried
EXEC sp_attach_single_file_db @.dbname = N'restored',
@.physname = N'E:\SQLdata\MSSQL\Data\restore\restored.mdf'
But when i run this i get:
Server: Msg 5105, Level 16, State 4, Line 1
Device activation error. The physical file name
'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
Server: Msg 1813, Level 16, State 1, Line 1
Could not open new database 'restore_AVC'. CREATE DATABASE is aborted.
Device activation error. The physical file name
'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
It looks like it is looking for the original logfile, i guess because this
database was never actually detached.Attaching databases that were not detached properly is unreliable, and does
not always work.
If there were open transactions, you will need to use sp_attach_db instead
(as these transactions will need to be rolled forward when the database is
attached). You will therefore need access to the log.
John.
hogwart wrote:
> Sorry if this is the wrong place to ask this. I have a file
> "restored.mdf", that was simply copied from the disk by backup
> software, i need the data inside that file. It is 12GB. the file
> was backed up from E:\SQLData\MSSQL\Data\ABC.mdf, and is now in
> E:\SQLData\MSSQL\Data\restore\restored.mdf.
> Is there any way to attach this database to any SQL Server 2000
> Server ? If so, i can then just copy the data from one table to
> another. I tried
> EXEC sp_attach_single_file_db @.dbname = N'restored',
> @.physname = N'E:\SQLdata\MSSQL\Data\restore\restored.mdf'
> But when i run this i get:
> Server: Msg 5105, Level 16, State 4, Line 1
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> Server: Msg 1813, Level 16, State 1, Line 1
> Could not open new database 'restore_AVC'. CREATE DATABASE is aborted.
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> It looks like it is looking for the original logfile, i guess because
> this database was never actually detached.|||Give the suggestion here a try (no guarantees). Make a backup of your files
first though.
http://tinyurl.com/3s4b6
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backups? Use MiniSQLBackup Lite, free!
"hogwart" <hogwart@.dannyland.com> wrote in message
news:%230tcOwzlEHA.3156@.TK2MSFTNGP12.phx.gbl...
> Sorry if this is the wrong place to ask this. I have a file
"restored.mdf",
> that was simply copied from the disk by backup software, i need the data
> inside that file. It is 12GB. the file was backed up from
> E:\SQLData\MSSQL\Data\ABC.mdf, and is now in
> E:\SQLData\MSSQL\Data\restore\restored.mdf.
> Is there any way to attach this database to any SQL Server 2000 Server ?
If
> so, i can then just copy the data from one table to another. I tried
> EXEC sp_attach_single_file_db @.dbname = N'restored',
> @.physname = N'E:\SQLdata\MSSQL\Data\restore\restored.mdf'
> But when i run this i get:
> Server: Msg 5105, Level 16, State 4, Line 1
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> Server: Msg 1813, Level 16, State 1, Line 1
> Could not open new database 'restore_AVC'. CREATE DATABASE is aborted.
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> It looks like it is looking for the original logfile, i guess because this
> database was never actually detached.
>

Attach a DB that was never Detached

Sorry if this is the wrong place to ask this. I have a file "restored.mdf",
that was simply copied from the disk by backup software, i need the data
inside that file. It is 12GB. the file was backed up from
E:\SQLData\MSSQL\Data\ABC.mdf, and is now in
E:\SQLData\MSSQL\Data\restore\restored.mdf.
Is there any way to attach this database to any SQL Server 2000 Server ? If
so, i can then just copy the data from one table to another. I tried
EXEC sp_attach_single_file_db @.dbname = N'restored',
@.physname = N'E:\SQLdata\MSSQL\Data\restore\restored.mdf'
But when i run this i get:
Server: Msg 5105, Level 16, State 4, Line 1
Device activation error. The physical file name
'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
Server: Msg 1813, Level 16, State 1, Line 1
Could not open new database 'restore_AVC'. CREATE DATABASE is aborted.
Device activation error. The physical file name
'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
It looks like it is looking for the original logfile, i guess because this
database was never actually detached.
Attaching databases that were not detached properly is unreliable, and does
not always work.
If there were open transactions, you will need to use sp_attach_db instead
(as these transactions will need to be rolled forward when the database is
attached). You will therefore need access to the log.
John.
hogwart wrote:
> Sorry if this is the wrong place to ask this. I have a file
> "restored.mdf", that was simply copied from the disk by backup
> software, i need the data inside that file. It is 12GB. the file
> was backed up from E:\SQLData\MSSQL\Data\ABC.mdf, and is now in
> E:\SQLData\MSSQL\Data\restore\restored.mdf.
> Is there any way to attach this database to any SQL Server 2000
> Server ? If so, i can then just copy the data from one table to
> another. I tried
> EXEC sp_attach_single_file_db @.dbname = N'restored',
> @.physname = N'E:\SQLdata\MSSQL\Data\restore\restored.mdf'
> But when i run this i get:
> Server: Msg 5105, Level 16, State 4, Line 1
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> Server: Msg 1813, Level 16, State 1, Line 1
> Could not open new database 'restore_AVC'. CREATE DATABASE is aborted.
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> It looks like it is looking for the original logfile, i guess because
> this database was never actually detached.
|||Give the suggestion here a try (no guarantees). Make a backup of your files
first though.
http://tinyurl.com/3s4b6
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backups? Use MiniSQLBackup Lite, free!
"hogwart" <hogwart@.dannyland.com> wrote in message
news:%230tcOwzlEHA.3156@.TK2MSFTNGP12.phx.gbl...
> Sorry if this is the wrong place to ask this. I have a file
"restored.mdf",
> that was simply copied from the disk by backup software, i need the data
> inside that file. It is 12GB. the file was backed up from
> E:\SQLData\MSSQL\Data\ABC.mdf, and is now in
> E:\SQLData\MSSQL\Data\restore\restored.mdf.
> Is there any way to attach this database to any SQL Server 2000 Server ?
If
> so, i can then just copy the data from one table to another. I tried
> EXEC sp_attach_single_file_db @.dbname = N'restored',
> @.physname = N'E:\SQLdata\MSSQL\Data\restore\restored.mdf'
> But when i run this i get:
> Server: Msg 5105, Level 16, State 4, Line 1
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> Server: Msg 1813, Level 16, State 1, Line 1
> Could not open new database 'restore_AVC'. CREATE DATABASE is aborted.
> Device activation error. The physical file name
> 'e:\SQLdata\MSSQL\data\ABC.ldf' may be incorrect.
> It looks like it is looking for the original logfile, i guess because this
> database was never actually detached.
>
sql

Monday, March 19, 2012

Assigning values to multiple variables (via subqueries) for use in an update

Hi, figured out where I was going wrong in my post just prior, but is
there ANY way I can assign several variables to then use them in an
Update statement, for example (this does not work):

ALTER PROCEDURE dbo.UpdateXmlWF
(
@.varWO varchar(50)
)
AS
DECLARE @.varCust VARCHAR(50)
SELECT @.varCust = (SELECT Customer FROM tblWorkOrders
WHERE WorkOrder=@.varWO)

DECLARE @.varAssy VARCHAR(50)
SELECT @.varAssy=(SELECT Assy FROM tblWorkOrders
WHERE WorkOrder=@.varWO)

UPDATE statement here using declared variables...

I can set one @.variable but not multiple. Any clues? kinda new to
this.

Thanks,
KathyKathy,

See my response to your previous post.

I think you want to try this instead:

DECLARE @.varCust varchar(50), varAssy varchar(50)
SELECT @.varCust = Customer,
@.varAssy = Assy
FROM tblWorkOrders
WHERE WorkOrder = @.varWO

UPDATE table
SET field = @.varCust,
field2 = @.varAssy
where somefield = somevalue

In fact, you could condense this to the following:

UPDATE table
SET field = tblWorkOrders.Customer,
field2 = tblWorkOrders.Assy
FROM tblWorkOrders
JOIN table ON tblWorkOrders.PK = table.FK
WHERE tblWorkOrders.WorkOrder = @.varWO
AND otherconditions...

Hope this helps.

--
-Chuck Urwiler, MCSD, MCDBA
http://www.eps-software.com|||Kathy,

You don't need two separate SELECT statements to assign values to your
variables. You can do it like this:

DECLARE @.varCust VARCHAR(50),
@.varAssy VARCHAR(50)

SELECT @.varCust = Customer,
@.varAssy = Assy
FROM tblWorkOrders
WHERE WorkOrder = @.varWO

But I don't understand your problem with inserting. You can modify as many
columns as you want in UPDATE statement. What's your problem here?
By the way, if these variables have been declared just to use in your UPDATE
statement, then you don't really need them. You can join tblWorkOrders table
with whatever table you want update and fetch values from tblWorkOrders
directly into your destination table.

Shervin

"KathyB" <KathyBurke40@.attbi.com> wrote in message
news:75e8d381.0310030718.82cad7f@.posting.google.co m...
> Hi, figured out where I was going wrong in my post just prior, but is
> there ANY way I can assign several variables to then use them in an
> Update statement, for example (this does not work):
> ALTER PROCEDURE dbo.UpdateXmlWF
> (
> @.varWO varchar(50)
> )
> AS
> DECLARE @.varCust VARCHAR(50)
> SELECT @.varCust = (SELECT Customer FROM tblWorkOrders
> WHERE WorkOrder=@.varWO)
> DECLARE @.varAssy VARCHAR(50)
> SELECT @.varAssy=(SELECT Assy FROM tblWorkOrders
> WHERE WorkOrder=@.varWO)
> UPDATE statement here using declared variables...
> I can set one @.variable but not multiple. Any clues? kinda new to
> this.
> Thanks,
> Kathy

Saturday, February 25, 2012

aspx vb sql parameter passing

Hello,
Can someone kindly point out what is wrong with the following code file. I'm trying to:
- fill a dropdown from a db on page load (this works!)
- when user selects from the list and hits a button, pass the dropdown value to a second query
- use the second query to make another call to the db and fill a data grid

In the code below, if I swap an actual value (eg '1005') into the command and comment out the .Parameter.Add statements, the dategrid is filled sucessfully. Otherwise, when the button is pressed, nothing is displayed.

Thanks

PS comments about my coding approach are welcome - I'm new to aspx...


<%@. Language="VBScript" Debug="true"%>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.data.SqlClient" %>
<html>
<head>
<script language="vb" runat="server">
Dim dbConnection As SqlConnection
Dim ds_Teams,ds_Agents As DataSet
Dim sqlCmd_Teams,sqlCmd_Agents As SqlDataAdapter

Dim dbConn = "server=csacd01;uid=mpierce;pwd=cabledog;database=clearviewacd"

Dim sql_select_teams = "" & _
"SELECT team_no, team_name " & _
"FROM dbo.team " & _
"WHERE (team_status = 'Curr')"

Dim sql_select_agents = "" & _
"SELECT last_name + ', ' + first_name AS name, agent_no " & _
"FROM dbo.users " & _
"WHERE (team_no = @.Team) AND (NOT (agent_no = '1029')) " & _
" AND (NOT (last_name IS NULL)) " & _
" AND (NOT (first_name IS NULL)) " & _
" AND (NOT (first_name = 'FirstName')) " & _
"ORDER BY last_name"

Dim teamList = "teamList"
Dim agentList = "agentList"

Sub Page_Load(Sender As Object, E As EventArgs)
if not (IsPostBack)
ds_Teams = new DataSet()
dbConnection = New SqlConnection(dbConn)
sqlCmd_Teams = New SqlDataAdapter(sql_select_teams, dbConnection)
sqlCmd_Teams.Fill(ds_Teams, teamList)
dbConnection.close()

dropdownlist_Teams.DataSource=ds_Teams.Tables(teamList).DefaultView
dropdownlist_Teams.DataBind()
end if
End Sub

sub Get_Agents(Sender As Object, E As EventArgs)
ds_Agents = new DataSet()
dbConnection = New SqlConnection(dbConn)
sqlCmd_Agents = new SqlDataAdapter(sql_select_agents, dbConnection)

sqlCmd_Agents.SelectCommand.Parameters.Add(new SqlParameter("@.Team", SqlDbType.NVarChar,4))
sqlCmd_Agents.SelectCommand.Parameters("@.Team").Value = dropdownlist_Teams.DataValueField

sqlCmd_Agents.Fill(ds_Agents,agentList)

dbConnection.close()

datagrid_Agents.DataSource=ds_Agents.Tables(agentList).DefaultView
datagrid_Agents.DataBind()
end sub

</script>
</head>
<body>
<form runat="server">
<asp:DropDownList id="dropdownlist_Teams" runat="server"
DataTextField="team_name"
DataValueField="team_no">
</asp:DropDownList
<input type="submit" onserverclick="Get_Agents" value="Get Agents" runat="server"><br /
<ASP:DataGrid id="datagrid_Agents" runat="server"
Width="500"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>
</form>
</body>
</html>

(1) dim a variable as string and then assign the sql stmt to the variable.
xample :
dim str1 as string
str1="Select..."

(2) is the valuefield numeric or string type ? also you might want to move the assigning of the str stmt to the get_events event itself rather than declaring it as a global string.

hth

Monday, February 13, 2012

ASP.NET mini Form with SQL Server 2000

Hi, i have created a mini form to test out easy steps and my sqlconnection but something is wrong with my connection i think...
I am running windows XP Pro SP 2. i have Microsoft VisStudio .NET 2003fully installed and now im installing SQL Server 2000 DeveloprsEdition. I just cimpleted the installaion and picked some settings:
Local Computer Installation, Create new Instance since this is thefirst time i use it, Server and Client Tools, and chose a Local ratherthe Domain option aferwards. (i have used this version before but hadto format the comp so now reinstalling everything).
My problem is this, i basically started the server and it on online. iwent to the "enterprise manager" and in the "Microsoft SQL Servers-> SQWL Server Group -> (Local)(Windows NT)-> Databases, icreated a new database called tables and added a single table just tobegin testing to see if at least its functional. i was able to do thisso far.
Now i started my .NET, and wrote a tiny program to see if theconnection to the SQL database would open, but it didnt. It alwaysfailed. i am programming an ASP.NET WebApplication. I cant open theconnection to my database. i dont know why. (The tiny piece of code iwrote is at the bottom in case you know c#). There is a note alsounder the code for the ones who will read through its simlicity
any help or walk through would be appreciated. thank u...
Sam



CODE:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebTest1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
string connection = ConfigurationSettings.AppSettings["ConnectionString"];


protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label outputBox;
protected System.Web.UI.WebControls.Label Label3;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
outputBox.Text = "Button has been clicked";
SqlConnection conn = new SqlConnection(connection);

outputBox.Text += "------Creating sql Connection";

try
{
conn.Open();
outputBox.Text += "-----opening the Connection";
}
catch(Exception p)
{
outputBox.Text += "-----failed to open Connection";
Console.WriteLine(p.ToString());
}
outputBox.Text += "----Success Connection and Login to Server!";

}
}
}


NOTE: i always get this output :
Button has been clicked------Creating sqlConnection-----failed to open Connection----SuccessConnection and Login to Server!
it skips the try block's print line which means it didnt work.....

string connection =ConfigurationSettings.AppSettings["ConnectionString"]; this one iupdated in my Web.config and included application setting line thatloox like this
<add key="ConnectionString" value="Data Source=127.0.0.1; Initial Catalog=login; User Id=sa; password=pass" />

You don't say what the error message is. Probably because you are catching it and not dealling with it. In your catch block you should output the p.Message to the outputBox for debugging purposes - Writing that to the console is not going to do any good as there isn't a Console.

You said you created a database called "tables". But your connection string is attempting to open a database called "login". If you do not have a database called "login" then the connection will fail.
I've never attempted to set the datasource to "127.0.0.1" before, so I cannot say if that is likely to be a problem. If I am connecting to the SQL Server on the local machine I always use "(local)"
Does this help?

|||

swy000 wrote:


string connection = ConfigurationSettings.AppSettings["ConnectionString"]; this one i updated in my Web.config and included application setting line that loox like this
<add key="ConnectionString" value="Data Source=127.0.0.1; Initial Catalog=login; User Id=sa; password=pass" />


I am not sure why you are using the loopback IP address 127.0.0.1 in your connection string when you can just use local host. In C# check out Programming .NET by Jeff Prosise for good sample code for Asp.net and the book written by the people who write the code for this site Coding strategies of the Asp.net team by Rob Howard and mathew Gibbs, for sample code for web applications in C#. Try the link below for sample connection strings. Hope this helps.
http://www.connectionstrings.com/