Showing posts with label retrieve. Show all posts
Showing posts with label retrieve. Show all posts

Wednesday, March 28, 2012

How do i ?! Basic Select Statement

I have an sql data source..select command is something like this.

Select password from users where username = Username.Text

how can i retrieve the password value and save it into a string?! to make a comparison between password value and a textbox Password.Text

i know its a silly question but i took me long time and i still can't find the ans

We are going to need a little more context to evaluate your exact situation, and how to do what you ask, but here's 1 method:

Dim sqlConn as new SqlConnection("connection string here")

Dim sqlCmd as new SqlCommand("SELECT password from users where username = @.username", SqlConn)

sqlCmd.Paramaters.AddwithValue("@.username", username.text)

Dim DA as new SqlDataAdapter(SqlCmd)

Dim DT as new DataTable

DA.Fill(DT)

Dim myString as string = DT.rows(0).item("password").value ' This line may not be totally correct, doing it from memory and will edit it later

If myString.tolower = stringToCompare.tolower then

'Password is valid

else

'Password could be valid - but something threw you into the else statment, maybe an empty string.

end if

|||

Connection string and select command are already defined in the SqlDataSource.

how can you use the SqlDataSource to extract the value of the password?

i tried to use a hidden GridView but i didn't know how to access a specific cell

by the way i am using C#

|||

The above example is a much cleaner method of getting information like this. Putting a hidden gridview on your page is a VERY heavy method of trying to extract a single value, but it can be done.

Please see this article on how to extract a Datatable from the SqlDataSource control. Once you've done that, you can access a specific row of your datatable as mentioned above.

http://msmvps.com/blogs/egoldin/archive/2006/12/27/how-to-get-datatable-out-of-sqldatasource.aspx

I really have to recomend against this method though. If you insist on using the SqlDataSouce control to get this done, consider putting two hidden textbox on the page, and binding the value that comes back from the database to the hidden textbox, then use a compare validator and only let the user continue if page.isvalid.

How do i ?! Basic Select Statement

I have an sql data source..select command is something like this.

Select password from users where username = Username.Text

how can i retrieve the password value and save it into a string?!

to make a comparison between password value and a textbox Password.Text

i know its a silly question but i took me long time and i still can't find the answer

Hello Ahmad,

Here's some code that should help you out... I'd be happy to clarify anything if needed.

using System.Data;

using System.Data.SqlClient;

string userPassword;

string userName;

SqlConnection cnn = new SqlConnection("Data Source = (local); Initial Catalog = nameofdatabase; Integrated Security = True");

SqlCommand cmd = new SqlCommand("select Password From Users Where username = @.username", cnn);

SqlParameter UserName = new SqlParameter("@.username", userName);

cmd.Parameters.Add(UserName);

cnn.Open();

string password = (string)cmd.ExecuteScalar();

cnn.Close();

cnn.Dispose();

if (userPassword == password)

{

// success! Do something here

}

else

{

// Failure...

}

Regards.

sql

Monday, March 26, 2012

How Detect SMO is installed?

Hi all

I'm using Microsoft.SqlServer.Management.Smo namespace for retrieve a list of avaible SQL Servers.

The problem is when SMO is not installed in the client machine. The application crashes although I put the command in a try - catch block.

try { DataTable dtSQLServers = SmoApplication.EnumAvailableSqlServers(false); }

catch { notInstalled = true }

Are there any easy way to know if SMO is installed , so i can advice the user that he must install SMO first?

Thanks.

You either have to lookup the file references assembly or use a component to check the reference to the GAC: http://www.codeproject.com/dotnet/Assemblydependencies.asp?df=100&forumid=31276&exp=0&select=977211

HTH, Jens Suessmeyer.

http://www.sqlserver2005.de

Friday, March 23, 2012

How could I retrieve info about all the jobs using SQL query?

How could I retrieve info about all the jobs using SQL query?
-D
See msdb.dbo.sp_help_job in BOL.
AMB
"D'Animal" wrote:

> How could I retrieve info about all the jobs using SQL query?
> -D
|||Start with:
select * from msdb..SysJobs
From there there is also SysJobSteps, SysJobHistory, etc.
"D'Animal" <D'Animal@.discussions.microsoft.com> wrote in message
news:0AA55366-FC84-46AF-9A3B-C6FEE21D2184@.microsoft.com...
> How could I retrieve info about all the jobs using SQL query?
> -D
|||I got it. Thank you.
-D
"Alejandro Mesa" wrote:
[vbcol=seagreen]
> See msdb.dbo.sp_help_job in BOL.
>
> AMB
> "D'Animal" wrote:

How could I retrieve info about all the jobs using SQL query?

How could I retrieve info about all the jobs using SQL query?
-DSee msdb.dbo.sp_help_job in BOL.
AMB
"D'Animal" wrote:

> How could I retrieve info about all the jobs using SQL query?
> -D|||Start with:
select * from msdb..SysJobs
From there there is also SysJobSteps, SysJobHistory, etc.
"D'Animal" <D'Animal@.discussions.microsoft.com> wrote in message
news:0AA55366-FC84-46AF-9A3B-C6FEE21D2184@.microsoft.com...
> How could I retrieve info about all the jobs using SQL query?
> -D|||I got it. Thank you.
-D
"Alejandro Mesa" wrote:
[vbcol=seagreen]
> See msdb.dbo.sp_help_job in BOL.
>
> AMB
> "D'Animal" wrote:
>

How could I retrieve info about all the jobs using SQL query?

How could I retrieve info about all the jobs using SQL query?
-DSee msdb.dbo.sp_help_job in BOL.
AMB
"D'Animal" wrote:
> How could I retrieve info about all the jobs using SQL query?
> -D|||Start with:
select * from msdb..SysJobs
From there there is also SysJobSteps, SysJobHistory, etc.
"D'Animal" <D'Animal@.discussions.microsoft.com> wrote in message
news:0AA55366-FC84-46AF-9A3B-C6FEE21D2184@.microsoft.com...
> How could I retrieve info about all the jobs using SQL query?
> -D|||I got it. Thank you.
-D
"Alejandro Mesa" wrote:
> See msdb.dbo.sp_help_job in BOL.
>
> AMB
> "D'Animal" wrote:
> > How could I retrieve info about all the jobs using SQL query?
> >
> > -Dsql

Monday, March 19, 2012

How can you use a variable tablename and retrieve the output from the Insert?

We are trying to create a unique key from a table with indentity set in the table. We will have a number of these tables. Therefore, we will be creating a stored procedure and passing the table as a parameter. In this example we are setting the table.

When we run the the script, the output clause from the insert should give us a unique number from the given table in the temporary table. This example stores the output in a temporary table @.tTemp.

How can you use a variable table name and retrieve the output from the Insert?

declare @.tTestTable varchar (20)

set @.tTestTable = 'mis.test_sequence'

--DECLARE @.tTestTable TABLE ( sqVal [int] IDENTITY(1,1) NOT NULL, add_date datetime)

declare @.testsql varchar (4000), @.testseq int

DECLARE @.tTemp table (mainpk int)

set @.testsql = 'DECLARE @.tTemp table (mainpk int) INSERT ' + @.tTestTable + ' OUTPUT INSERTED.sqVal into @.tTemp VALUES (getdate() ) SELECT @.testseq=mainpk FROM @.tTemp'

select @.testsql

EXECUTE sp_executesql @.testsql, N'@.testseq int output,@.tTemp table (mainpk int),@.tTemp table (mainpk int) ',@.tTemp,@.tTemp,@.testseq output,@.tTemp

SELECT * FROM @.tTemp

Please help

Thanks Tim.

Why not to create an sp per each table, instead trying to come with a general one?

Code Snippet

use tempdb

go

create table #t (c1 int not null identity, c2 datetime)

declare @.tTestTable varchar (20)

set @.tTestTable = '#t'

declare @.testsql nvarchar (4000), @.testseq int

set @.testsql = 'INSERT into' + quotename(@.tTestTable) + '(c2) values(getdate()); set @.testseq = scope_identity()'

select @.testsql

EXECUTE sp_executesql @.testsql, N'@.testseq int output',@.testseq output

SELECT @.testseq

drop table #t

The Curse and Blessings of Dynamic SQL

http://www.sommarskog.se/dynamic_sql.html

AMB|||

Thankyou hunchback,

Your Code Snippet helped me solve my problem.

Tim.

Here's my final code.

USE [TestDB]

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

Create proc GetNext (@.sequenceName varchar(40) , @.nextVal int output)

as

begin

declare @.sqlStmt nvarchar (4000)

declare @.tTemp table (mainpk int)

set nocount on

-- This option works using the OUTPUT clause.

set @.sqlStmt = 'DECLARE @.tTemp table (mainpk int) Insert ' + @.sequenceName + ' OUTPUT INSERTED.sqVal into @.tTemp DEFAULT VALUES SELECT @.nextVal=mainpk FROM @.tTemp'

execute sp_executesql @.sqlStmt, N'@.nextVal int output',@.nextVal output

set nocount off

end