SqlException: The multi-part identifier "chamin#gmail.com" could not be bound - sql-server

When i try to execute the query in given bellow
is worked correctly and give the result
but when i change id into email then it will raise the exception
employee = connection.Query<Employee>("select id, email as email , password as password, role as role from Employee where email="+"chamin#gmail.com");
how do i fix this problem??

Strings have to be quoted in SQL :
employee = connection.Query<Employee>(
"select id, email as email , password as password, role as role from Employee where email='"+"chamin#gmail.com" + "'"
);
But your code is very permissive to SQL injection. You should use SQL parameters : it will prevent injection and avoid the issue you met.

Related

Snowflake Query returns Http status: Unprocessable Entity

I'm able to successfully connect to the Snowflake database through my .NET app, but I'm unable to run a SQL command due to the following error from the Snowflake:
Message: Http status: UnprocessableEntity
ResponseContent:
"code" : "391920",
"message" : "Unable to run the command. You must specify the warehouse to use by either setting the warehouse field in the body of the request or by setting the DEFAULT_NAMESPACE property for the current user.",
"sqlState" : "57P03",
"statementHandle" : "01a8
Here is my code I'm using.
public async Task<QueryResult> QuerySnowflake(string statement, string database, string schema)
{
var content = new
{
statement,
database,
schema
};
return await _httpClient.SnowflakePost<QueryResult>($"https://{_accountId}.snowflakecomputing.com/api/v2/statements", content, await GetHeaders(), _cancellationToken);
}
statement = SELECT * FROM SNOWFLAKE_SAMPLE_DATA.TPCH_SF1.CUSTOMER
database = SNOWFLAKE_SAMPLE_DATA
schema = TPCH_SF1
I have already tried the following:
ALTER USER my_username SET DEFAULT_NAMESPACE = SNOWFLAKE_SAMPLE_DATA.TPCH_SF1
GRANT SELECT ON ALL TABLES IN SCHEMA "TPCH_SF1" TO ROLE sysadmin
ALTER USER my_username SET DEFAULT_ROLE = sysadmin
All of these did not change the error response.
I don't think it needs a code change as it works with other Snowflake accounts (I'm using a new trial account). I believe I have my something wrong with my account (e.g. missing role, missing warehouse, missing permission, etc).
Any help would be very much appreciated.
The user does not have a default warehouse and none is specified in the connection request or a use command in the session. You can try sending this command before running your select:
use warehouse MY_WAREHOUSE;
You can also specify it in the connection, or specify a default for the user:
ALTER USER MY_USER SET DEFAULT_WAREHOUSE = MY_WAREHOUSE;

TSQL: access user data from LDAP

I have an Active Directory (LDAP) that stores user information. I'm trying to access it using TSQL, but I'm having authentication problems.
The LDAP looks like this (I edited the data):
The user has the following properties:
Now, I'm trying to get the info from this user through a TSQL query from SQL Server using OPENROWSET like so:
SELECT *
FROM
OPENROWSET('ADSDSOObject','adsdatasource'; 'domain.com.io\test';'the_sha1_pass',
'SELECT givenname
FROM ''LDAP://domain.com.io/ou=Users,
dc=domain,dc=com,dc=io'' WHERE givenname = ''Test''
')
But when I execute it I get the following error:
Server: Msg 7399, Level 16, State 1, Line 1 OLE DB provider '
ADSDSOObject ' reported an error. The provider indicates that the user
did not have the permission to perform the operation.
Now, I don't know if I have to send the authentication of the user I'm trying to query over, or the credentials I use to connect with LDAPAdmin. And if I have to send it, should I send the pass already encrypted or without encryption?
Thanks.
You can change your OPENROWSET query as follows by removing the username and password
SELECT * FROM OPENROWSET('ADSDSOObject','adsdatasource',
'SELECT givenname
FROM ''LDAP://kodyaz.com/DC=kodyaz,DC=com'' WHERE givenname = ''KODYAZ''
')
Or as an alternative, you can try following query structure
SELECT * FROM OpenQuery(ADSI, 'SELECT * FROM ''LDAP://kodyaz.com/DC=kodyaz,DC=com'' WHERE objectCategory=''user'' AND givenname = ''KODYAZ'' ')
I added user objectCategory in WHERE clause with givenname parameter in OpenQuery command
Before running above Select statement, you can add LDAP as linked server in SQL Server as shown in referred tutorial
EXEC sp_addlinkedserver 'ADSI', 'Active Directory Service Interfaces', 'ADSDSOObject', 'adsdatasource'

How to check if string a exists in a specified column in database

this is my sample access database:
ID--UserName--Password--AccountType
1---- A123 --1234 --User
2-----B123 --1345 --Admin
I am using VS2012. In my VB.net Project I have username textbox, a password textbox,
and login button.
I add my database using a wizard. I can add, modify, delete, and query, but how to check if the entered username in username text box exists in UserName column?
I filled up my dataset using:
Me.UsersTableAdapter.Fill(Me.WSDataSet.users)
and if I want to get the user type I am using:
Me.WSDataSet.users.FindByUserName(IDtxt.Text).AcountType
but the main problem if user not exists I get the error below:
An unhandled exception of type 'System.NullReferenceException' occurred in user login.exe Additional information: Object reference not set to an instance of an object.
How can I check if the username exists or not?
Try doing this.
Dim user = Me.WSDataSet.users.FindByUserName(IDtxt.Text)
If not user is nothing Then
'Do what you want with the user object
Else
'Message User does not exist.
End If
you just check if the user exists then do what you want with it.

Can't select User ID from Salesforce

I'm trying to select user's subordinates from Salesforce, but a simple query
SELECT Id FROM User WHERE ManagerId=xxxxxxxxx
returns bunch of null values, when I run
SELECT Id,Name FROM User WHERE ManagerId=xxxxxxxx
I get the correct names, still no IDs.
Is this a permission issue? I can't find anything when I login to portal.
I'm running the queries via API on Sandbox environment.
Try this (both works for me allways):
Id myId = [Select Id From User Where Username = 'myUserName'].Id;
System.debug('#### myId: ' + myId);
List<User> myIdList = [Select Id From User Where Username = 'myUserName' Limit 1];
System.debug('#### myId from list: ' + myIdList[0].Id);
Portal Licence doesn't allow to query User. However you have still access to the name of the user through OwnerId, CreatedById, LastModifiedById using in an inputfield.
i.e :
If you want to have access to user through the portal you need a custom object and synchronise your records with User by trigger.

display the active member details

I have one database which contains username, password and IsActive (data type is "bit") columns.
My requirement is that I have to display the user details when the user name and password is correct and when the active column of that user is true.
How can I check these 3 columns in Sql Server 2005?
How about something like the following?
SELECT username, password
FROM UsersTable
WHERE IsActive > 0 AND username = 'admin' AND password = '1234'

Resources