Click here to Skip to main content
15,923,273 members
Home / Discussions / Database
   

Database

 
GeneralRe: ADO + SQL statements Pin
#realJSOP22-Aug-04 23:35
professional#realJSOP22-Aug-04 23:35 
GeneralRe: ADO + SQL statements Pin
Christian Graus23-Aug-04 10:23
protectorChristian Graus23-Aug-04 10:23 
GeneralRe: ADO + SQL statements Pin
Ryan Roberts23-Aug-04 4:44
Ryan Roberts23-Aug-04 4:44 
GeneralRe: ADO + SQL statements Pin
J. Vorstenbosch25-Aug-04 0:40
J. Vorstenbosch25-Aug-04 0:40 
GeneralAuto date colum Pin
Zee_Zee21-Aug-04 1:55
Zee_Zee21-Aug-04 1:55 
GeneralRe: Auto date colum Pin
David Salter22-Aug-04 11:43
David Salter22-Aug-04 11:43 
GeneralSQL database problem Pin
Chodici Mrkev20-Aug-04 4:04
Chodici Mrkev20-Aug-04 4:04 
GeneralRe: SQL database problem Pin
Colin Angus Mackay20-Aug-04 4:49
Colin Angus Mackay20-Aug-04 4:49 
First and foremost - For SECURITY. NEVER pass a value directly from a user control in to a SQL Command. This is suseptable to injection attack. What would happen if, in the txtLogin box, I had written
''; shutdown with nowait;


The answer is that your CommandText would fully read:
SELECT COUNT(*) FROM Users WHERE login=''; shutdown with nowait;
This would run your SELECT and then immediately terminate your SQL Server Process - any other queries would fail and no new connections would be permitted. And you (or your DBA) would have to manually restart the SQL Server.

Okay - now that you know more about security I'll get on with your question...

I would always interact with the database using stored procedures. You add a layer of security there if you only permit access to the stored procedures, then all you can ever do is what the stored procudures can do.

CREATE PROCEDURE RegisterUser(
   @UserName   varchar(64),
   @Password   carchar(64)
)
AS

IF EXISTS(SELECT * FROM Users WHERE login=@UserName)
BEGIN
   RAISERROR('The user '+@UserName+' already exists', 16, 1);
   RETURN;
END

INSERT INTO Users (login, password)
VALUES(@UserName, @Password)
GO


Then from your .NET application you can do something like this

SqlCommand cmd = new SqlCommand("RegisterUser", vilemConn);
cmd.Parameters.Add(new SqlParameter("@UserName", txtLogin.Text);
cmd.Parameters.Add(new SqlParameter("@Password", txtPassword.Text); // Don't know what this really is.
try
{
   cmd.ExecuteNonQuery();
}
catch(SqlException sqlEx)
{
   // If the command is duplicated then sqlEx.Message will contain the message defined in the
   // RAISERROR in the stored procedure and the sqlEx.Number will be 50000 (for a custom error)
}


This SQL and .NET code will register a user if one does not already exist, if one does exist an error is raised which you can then handle as appropriate.

Sorry, but I can only read VB.NET code, I don't know enough VB.NET to create the VB code without referring to a lot of books. Hopefully you will see what I am trying to achieve with the C# code - remember it is the Framework and not the language that is important here.

You will also need to add as parameters and extend the stored procedure for the other items in your table.

Does this help?


"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!


GeneralRe: SQL database problem Pin
Chodici Mrkev20-Aug-04 6:09
Chodici Mrkev20-Aug-04 6:09 
GeneralRe: SQL database problem Pin
Colin Angus Mackay20-Aug-04 6:26
Colin Angus Mackay20-Aug-04 6:26 
GeneralRe: SQL database problem Pin
Chodici Mrkev20-Aug-04 7:33
Chodici Mrkev20-Aug-04 7:33 
GeneralRe: SQL database problem Pin
Colin Angus Mackay20-Aug-04 7:54
Colin Angus Mackay20-Aug-04 7:54 
GeneralRe: SQL database problem Pin
Steven Campbell20-Aug-04 7:42
Steven Campbell20-Aug-04 7:42 
GeneralRe: SQL database problem Pin
Colin Angus Mackay20-Aug-04 7:55
Colin Angus Mackay20-Aug-04 7:55 
GeneralRe: SQL database problem Pin
Christian Graus22-Aug-04 15:30
protectorChristian Graus22-Aug-04 15:30 
GeneralRe: SQL database problem Pin
Colin Angus Mackay23-Aug-04 4:12
Colin Angus Mackay23-Aug-04 4:12 
GeneralEnterprise level database access in C#/.net Pin
Salil Khedkar19-Aug-04 21:36
Salil Khedkar19-Aug-04 21:36 
GeneralRe: Enterprise level database access in C#/.net Pin
Christian Graus22-Aug-04 15:27
protectorChristian Graus22-Aug-04 15:27 
GeneralDisplaying a DataGrid in child Form Pin
abhishk2001@yahoo.com19-Aug-04 13:01
abhishk2001@yahoo.com19-Aug-04 13:01 
GeneralAssembly resource not found Pin
unosinu19-Aug-04 6:00
unosinu19-Aug-04 6:00 
GeneralRe: Assembly resource not found Pin
Mekong River21-Aug-04 4:57
Mekong River21-Aug-04 4:57 
GeneralSQL query with parameter and % Pin
YomYom19-Aug-04 4:58
YomYom19-Aug-04 4:58 
GeneralRe: SQL query with parameter and % Pin
Chris Meech19-Aug-04 8:29
Chris Meech19-Aug-04 8:29 
GeneralRe: SQL query with parameter and % Pin
YomYom19-Aug-04 10:23
YomYom19-Aug-04 10:23 
GeneralRe: SQL query with parameter and % Pin
Looney Tunezez19-Aug-04 11:10
Looney Tunezez19-Aug-04 11:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.