|
There are some system catalogs that can help, but it varies from database vendor to database vendor. If you're using C++ there are some ATL DB client side helpers for this.
Steve S
Developer for hire
|
|
|
|
|
Some databases support INFORMATION_SCHEMA views you can query:
SELECT * FROM INFORMATION_SCHEMA.TABLES
then loop through the results (be sure to look at the field TABLE_TYPE to see what type of table it is
Now select the columns for each table:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Current table name'
|
|
|
|
|
Perfekt, this one was easy..
THX
|
|
|
|
|
Hi
I am developing a UserControl, I save this user control code to the database, which will be loaded dynamically to the Page.
In my user control code consist of following lines:
<asp:SqlDataSource ID="GetDataFromDatabase" runat="server" ConnectionString="<%$ ConnectionStrings:sConnectionString %>"
SelectCommand="SELECT field_defaultvalue FROM cp_productfields WHERE field_product_id='ProductID' AND field_type='DropDownList' AND field_Control_ID='ddVCAuflag'"
ProviderName="<%$ ConnectionStrings:sConnectionString.ProviderName %>"></asp:SqlDataSource>
At this moment, I am manually puting this data to data
base using "INSERT INTO" statement.
But I get following error:
Incorrect syntax near 'ProductID'.
This is because single quotation mark around ProductID (field_product_id='ProductID').
If I change this single quotation to double quotation (e.g. field_id="ProductID") then this works okay.
But in that scenario, SELECT statement fails while execution.
Can anyone please let me know how can I achieve both successfully?
Thanks in advance.
|
|
|
|
|
Did you try using :
"SELECT field_defaultvalue FROM cp_productfields WHERE field_product_id='" & ProductID & "' AND field_type='" & DropDownList & "' AND field_Control_ID='" & ddVCAuflag & "'"
??
thanks.
|
|
|
|
|
Hi,
I just have a couple of questions on the DAAB:
When using db.AddInParameter and the input parameter is an empty string, does it automatically add DBNull.Value to the coloumn? Or should I rather then create a method that checks for an empty string, if it is empty then add DBNull.Value to that coloumn (if it accepts NULL values).
How to get the returned value and throw an exception to the user? I usually have a @ReturnValue parameter, with ParameterDirection.ReturnValue. I was thinking of not doing it this way but rather something like:
using (dbCommand = db.GetStoredProcCommand(strSProc))
{
db.AddInParameter(dbCommand, "@CarName", DbType.String, objCar.Name);
// Rest of the parameters
IDataReader rdr = db.ExecuteReader(dbCommand);
if (rdr.Read())
{
// Do convertions here of what was returned from the stored procedure
}
}
Please can someone help advise.
Regards,
ma se
|
|
|
|
|
String or any parameter except int accept Null value for empty parameter.
Kumar
|
|
|
|
|
I have the following using block (not all code is displayed):
try
{
using (dbCommand = db.GetStoredProcCommand("sp_GetAllCars"))
{
rdr = db.ExecuteReader(dbCommand);
while (rdr.Read())
{
Car objCar = new Car();
objCar.ID = rdr.GetInt32(0); // Required
objCar.Name = rdr.GetString(1); // Required
// Add Car instance to array
carList.Add(objCar);
}
// Close the reader object
rdr.Close();
}
}
catch (SqlException ex)
{
HandleSQLError(ex, "sp_GetAllCars");
}
finally
{
dbCommand.Dispose();
}
What does the using block close and dispose of, because I am thinking that I am closing uneccessary stuff. I always thought that the using block only closed this that were declared in the brackets like:
using (...only closed what inserted here...)
{
...
}
Please can someone comment.
Regards,
ma se
|
|
|
|
|
Hi
I don't think you need the
ma se wrote: dbCommand.Dispose();
dbCommand.Dispose(); in the finally block because the using block would have released the dbcommand
|
|
|
|
|
Thanks I thought so, but what about db and rdr, must I still close rdr? Doesn't the using block take care of this?
|
|
|
|
|
Hi
i am not completely sure what you are trying to do but i would have implemented your code this way:
try
{
using (dbCommand = db.GetStoredProcCommand("sp_GetAllCars"))
{
rdr = dbCommand.ExecuteReader(); //db.ExecuteReader(dbCommand);
while (rdr.Read())
{
Car objCar = new Car();
objCar.ID = rdr.GetInt32(0); // Required
objCar.Name = rdr.GetString(1); // Required
// Add Car instance to array
carList.Add(objCar);
}
// Close the reader object
rdr.Close();
}
}
catch (SqlException ex)
{
HandleSQLError(ex, "sp_GetAllCars");
}
finally
{
}
you need to close rdr since it has exclusive access to the connection and without closing it, you can not use the underlying connection. at the end of the using, the dbCommand will be release. what i think you might need in the finally is to close the underlying connection. but that also depends on if you are not using the connection else where within the function after the finally block. i generally don't dispose my connections. but i use the Close() option since this will return the connection to the pool rather than releasing all its resources which happens with dispose().
-- modified at 5:31 Wednesday 12th July, 2006
|
|
|
|
|
What do you mean by the underlying connection? Is it the db? Do I have to close/ dispose of this db as well? If so where?
I defined db like this:
// Load the default database
Database db = DatabaseGlobals.GetDefaultDatabase;
|
|
|
|
|
If you use a using for the datbase connection and for the data reader then you don't need the calls to close or dispose, e.g.,
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
string text = "spReadCustomers";
SqlCommand command = new SqlCommand();
command.Connection = cn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = text;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int firstNameIdx = reader.GetOrdinal("FirstName");
int lastNameIdx = reader.GetOrdinal("LastName");
string firstName = reader.GetString(firstNameIdx);
string lastName = reader.GetString(lastNameIdx);
}
}
}
Kevin
|
|
|
|
|
Hi,
When updating a record in the database, for example, is it wise (or good practice) to always do a check to see if the record exists in the stored procedure?
Please can someone comment.
Regards,
ma se
|
|
|
|
|
Checking the record in the procedure for update is always secure. It doesn't take any extra overhead in your program but it is additional as well as perfect checking fot data concurrency
Kumar
|
|
|
|
|
how to write update SQL statement in order to remove all underline in my data for each field, example:
"HIGH COST___" change to "HIGH COST" which is remove the underline for all data..
Thanks
angela
|
|
|
|
|
Update table1 set col1 = replace(col1,'_','')
--
Don't take life seriously because you can't come out of it alive.
-Warren Miller
(From Monty2[^] bio)
|
|
|
|
|
ok, i get it
Thanks a lot for help, Dinuj Nath
angela
|
|
|
|
|
How about::
Update table1 set col1 = 'HIGH COST'
|
|
|
|
|
I think the previous 'replace' sql statement is more suitable for me because the data that I wanted to remove those underline have different value, not just for 'HIGH COST' only
Anyway, thanks again for your suggestion.;)
angela
|
|
|
|
|
Hello,
I have this sub:
Sub ConDB()
Dim oSqlCliCon As SqlConnection
oSqlCliCon = New SqlConnection("Data Source=120.260.9.8;Integrated Security=SSPI;Initial Catalog=pubs")
Try
oSqlCliCon.Open()
Catch ex As Exception
msgbox(ex.Message)
End Try
oSqlCliCon.Close()
End Sub
The problem:
The code works for VB6/VBNET/VBNET2003 for SQL SERVER
for VBNET2005 works only local but for the SQL SERVER don't.
The exception message:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The exception message if modify the connectionstring:
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Please help, thank you.
gangeles
|
|
|
|
|
I've been researching this but am confused on how to set up the syntax.
I have a stored procedure that bascially does the following:
1. Recieves two parameters: a part type, and a part number. The part number is actually a varchar.
2. Inserts the part type ID (taken from a second table called PartTypes) and Part Number into the table Parts.
3. The table Parts has a third field, ID which is an auto-incrmemented number. (Apparently the "Part Number" can change so we use this auto-incremented ID to reference the part number in other tables to keep things simple if the number changes.)
Okay, what I need to do is once that gets inserted into the Parts table, I need to use that Parts.ID that the system assigned. I need to send it back to the page that called it.
So two questions:
1. how do I set up this OUTPUT thing? The code I found online wasn't very helpful. Here is the stored proc:
CREATE PROCEDURE [dbo].[UpdatePart_Step_1]<br />
@PartNumber varchar (15),<br />
@PartTypeDescription varchar (60) <br />
<br />
AS<br />
declare @pt_id integer<br />
<br />
if NOT EXISTS (Select * from Parts where PartNumber = @PartNumber)<br />
BEGIN<br />
<br />
SELECT @pt_id = pt.ID FROM PartTypes pt <br />
<br />
INNER JOIN Parts p on pt.ID = p.PartTypeID<br />
<br />
WHERE pt.Description = @PartTypeDescription<br />
<br />
insert into Parts<br />
(PartNumber, PartTypeID)<br />
<br />
VALUES<br />
(<br />
@PartNumber,<br />
@pt_id<br />
)<br />
END<br />
return 0<br />
GO
2. How do I get this value back into the original page that called the stored proc?
My thinking is that I should create a completely separate stored proc to get this Parts.ID, but I'm not sure what the norm is.
Thanks again all!
|
|
|
|
|
<br />
CREATE PROCEDURE [dbo].[UpdatePart_Step_1]<br />
@PartNumber varchar (15),<br />
@PartTypeDescription varchar (60),<br />
@ID int OUTPUT<br />
AS<br />
declare @pt_id integer<br />
<br />
if NOT EXISTS (Select * from Parts where PartNumber = @PartNumber)<br />
BEGIN<br />
<br />
SELECT @pt_id = pt.ID FROM PartTypes pt<br />
<br />
INNER JOIN Parts p on pt.ID = p.PartTypeID<br />
<br />
WHERE pt.Description = @PartTypeDescription<br />
<br />
insert into Parts<br />
(PartNumber, PartTypeID)<br />
<br />
VALUES<br />
(<br />
@PartNumber,<br />
@pt_id<br />
)<br />
END<br />
SELECT @ID=@@IDENTITY<br />
return 0<br />
GO<br />
And here is the code from client side
<br />
Dim cmd as new SqlCommand("UpdatePart_Step_1", connection)<br />
cmd.CommandType=StoredProcedure<br />
cmd.Parameters.Add("@PartNumber", varchar, 15).Value="Anything"<br />
cmd.Parameters.Add("@PartTypeDescription", varchar, 60).Value="Anything else"<br />
cmd.Parameters.Add("@ID", int).Direction=ParameterDirection.Output<br />
cmd.ExecuteNonQuery()<br />
Dim res as integer=cmd.Parameters("@ID").Value -> Returning value from SP<br />
Hope that help
|
|
|
|
|
nguyenvhn wrote: cmd.Parameters.Add("@ID", int).Direction=ParameterDirection.Output
How would I do this in C#? This is my code that calls the stored proc:
ds2 = new DataSet();<br />
SqlParameter param1 = new SqlParameter("@PartNumber", strPartNumberInputReference); <br />
SqlParameter param2 = new SqlParameter("@DwgNumber", strDwgNumber);<br />
SqlParameter param3 = new SqlParameter("@DwgRevision", strDwgRevision);<br />
SqlParameter param4 = new SqlParameter("@DwgLocation", strDwgLocation);<br />
SqlParameter param5 = new SqlParameter("@PartTypeDescription", strPartTypeID);<br />
<br />
ds2=SqlHelper.ExecuteDataset(this.connectionString, CommandType.StoredProcedure,"dbo.UpdatePart_Step_1", param1, param5);<br />
dt2 = ds2.Tables[0];
Thanks again for your help!
|
|
|
|
|
Hey there.
I just installed SQL Server express 2005 yesterday and I'm trying to connect to it using C# on Win 2000 SP4. I've never used SQL server so this is probably a novice question.
I used the following connection string to connect
SqlConnection conn = new SqlConnection("Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs");
However, I get the following error:
Exception Occured -->> System.Data.SqlClient.SqlException: An error has occurred
while establishing a connection to the server. When connecting to SQL Server 2
005, this failure may be caused by the fact that under the default settings SQL
Server does not allow remote connections. (provider: Named Pipes Provider, error
: 40 - Could not open a connection to SQL Server)
I am running the SQL Server service. I see the sqlservr.exe in the taskbar but I'm not sure if it is actually running.
Anyone know how I can connect to the sql server from C#?
Thank you!
-K
|
|
|
|