I want to read the data from Access Database in order to check if a password is correct or not. I use this code:
var check=false;
OleDbCommand c = new OleDbCommand();
c.Connection = co //the connection to the Database;
c.CommandText = "select * FROM User Where user_name='"+usee+"'";
OleDbDataReader re = c.ExecuteReader();
while (re.Read())
{
if (re.ToString() == pasy)
{
check = true;
}
}
It gives me a "Syntax error in FROM clause." when the code executes.
"User" is a reserved word for Access.
Here is a list of reserved words :
http://support.microsoft.com/kb/286335/en-us
and here is a page describing that problem :
http://support.microsoft.com/kb/181489/en-us
Related
I understand AppMaker can be used with PostgreSQL and MySQL but the documentation says you can use your own data source. This is a bit ambiguous and I am trying to find out specifically if I can use an existing SQL Server database. I've spoken with Google's support team but they were unable to answer and directed me to ask a question here. Thanks in advance :-)
Quick answer is yes but Cloud SQL is easier and works better.
Long answer:
App Maker does support SQL using REST and JDBC however it is much harder than using their own CloudSQL model. some code for JDBC from when i last used it:
function get_data() {
var queryStr = 'SELECT * FROM users'; //SQL query
var calcRecords = []; //empty array to put records into
var connection = Jdbc.getConnection(dbUrl, user, userPwd); //connect to database
var statement = connection.prepareStatement(queryStr); //prepared statement
var results = statement.executeQuery(); //execute prepared query
try {
while (results.next()) {
var calcRecord = app.models.test.newRecord();
calcRecord.id = results.getInt(1); //data source test row id
calcRecord.user = results.getString(2); //data source test row user
calcRecord.passwd = results.getString(3); //data source test row passwd
calcRecords.push(calcRecord); //append to 2D array
}
results.close(); //close the results
statement.close(); //close the connection
} catch (err) {
console.error('SQL query failed: ' + err);
}
return calcRecords;
}
The equivalent Google Cloud SQL:
function get_users(){
var ds=app.datasource.Users.Items;
var data = [];
for(var i = 0; i < Items.length; i++){
tmp = [];
tmp.push(Items[i].id); //the id
tmp.push(Items[i].user); //the username
tmp.push(Items[i].password); //the password
data.push(tmp); //push to create 2D array
}
return data;
}
Doing query is a little harder and requires server code (so async callbacks), but populating Widgets like dropdown and implementing tables is a lot easier, usually requiring a couple of clicks rather than a lot of code.
How to map the OLE DB source SQL command query parameters with variables using EzAPI ?
Basically I need to do something like below.
Thanks in advance.
Here is how I had to do it for SSIS 2012. I had to find the GUID of the variable in question and set it that way.
EzOleDbSource source = new EzOleDbSource(this);
source.Connection = sourceconnection;
source.SqlCommand = sourcecomannd;
source.AccessMode = AccessMode.AM_SQLCOMMAND;
source.SetComponentProperty("ParameterMapping", "\"Parameter0:Input\",{C2BCD5B0-1FDB-4A74-8418-EEF9C1D19AC3};");
To get the GUID you can query the Variables in the EZPackage object.
Application a = new Application();
var package = a.LoadPackage(packagelocation, null);
var ezpackage = new EzPackage(package);
var firstOrDefault = ezpackage.Variables.OfType<Microsoft.SqlServer.Dts.Runtime.Variable>()
.AsQueryable()
.FirstOrDefault(x => x.Name.Equals("MyParameter"));
if (firstOrDefault != null)
{
var guid =
firstOrDefault.ID;
}
I have accepted Geek's answer because it is the real answer for my question. But I found instead of mapping variables to parameters we can use variables directly in the query. For example: exec your_sp_name "#[User::your_variable_name]"
UPDATE : Above method is not working.
Use the the accepted answer method. To take the GUID of the variable, I used the follwing method.
string guid = string.Empty;
foreach (var x in this.Variables)
{
if (x.Name == "cdc_capture_log_id")
{
guid = x.ID;
}
}
Where this = EzPackage. Same as above.
I executed the same query in my db interface directly and got two rows as result... I thought i should be getting same here too... but it is returning only one and for the second it says column index out of range 2>1 !!!
session= request.getSession();
String name=(String)session.getAttribute("user");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mutualfund", "root", "");
Statement statement = connection.createStatement();
String qry="SELECT `account_type_id` FROM `cust_accounts` WHERE `cust_id`=\""+name+"\"";
ResultSet rs = statement.executeQuery(qry) ;
if(!rs.next())
{
out.println("Server Down.. Please try again later ");
}
else {
String one=rs.getString(1);
String two=rs.getString(2);
}
Your code isn't asking for two rows, it's asking for two columns, and your query only supplies one.
if (!rs.next()) {
out.println("nope");
} else {
String one = rs.getString(1);
String two = "";
if (rs.next())
two = rs.getString(1);
}
}
Well im here because i have a problem. i have code that was created a while ago. my code is working properly when i a user type the correct username and password. so my problem is when a user insert wrong username and password error message wont show. Here is my code:
MyDs.Clear();
MyDa.SelectCommand = Conn.CreateCommand();
MyDa.SelectCommand.CommandText =
"select * from PersonalName where Firstname=#first and Lastname=#last";
MyDa.SelectCommand.CommandType = CommandType.Text;
MyDa.SelectCommand.Parameters.Add("#first", DbType.String, 25, "Firstname").Value = textbox_Username.Text;
MyDa.SelectCommand.Parameters.Add("#last", DbType.String, 25, "Lastname").Value = textbox_Password.Text;
MyDa.Fill(MyDs);
foreach (DataRow item in MyDs.Tables[0].Rows)
{
if (textbox_Username.Text != item[1].ToString() || textbox_Password.Text != item[3].ToString())
{
MessageBox.Show("not connected");
}
else
{
MessageBox.Show("Connected");
}
}
Anyone can tell me what is the problem with this code?
Your dataset will be empty if they put in the wrong username. This will be closer to what you want:
if (MyDs.Tables[0].Rows.Count > 0)
{
// you don't need to check username, the SQL query took care of that
if (textbox_Password.Text == item[3].ToString())
MessageBox.Show("Connected.");
else
MessageBox.Show("Failed."); // wrong password
}
else
MessageBox.Show("Failed."); // no such user
if the query returns no records becasue firast and last name aren't present, foreach doesn't execute nothing happens...
You need to test for myDa being empty as well, your code only works for checking valid users.
See this question. I have the following code that executes against a SQLIte database using a strongly typed dataset.
messagesAdapter.Update(messages);//messages is a DataTable
var connection = messagesAdapter.Connection;
var retrieveIndexCommand= connection.CreateCommand();
retrieveIndexCommand.CommandText = #"Select last_insert_rowid()";
connection.Open();
var index = retrieveIndexCommand.ExecuteScalar();
connection.Close();
This does not work as the last_inser_rowid() always returns zero. This caused by the fact that it needs to be called during the same connection that is used by the TableAdapter's Update command. How can I change the the TableAdapter's Insert or Update command so that it return the index?
If you are inserting a single row, you can use this:
// cast if necessary
using (var insert = (SQLiteCommand)this.Adapter.InsertCommand.Clone()) {
insert.CommandText += "; SELECT last_insert_rowid()";
foreach (SQLiteParameter parameter in insert.Parameters) {
parameter.Value = row[parameter.SourceColumn];
}
}
var index = Convert.ToInt32(insert.ExecuteScalar());
You can also use it to insert multiple rows and assign your id to each one:
using (var insert = (SQLiteCommand)this.Adapter.InsertCommand.Clone())
{
insert.CommandText += "; SELECT last_insert_rowid()";
// this filter only added rows
foreach (MyDataSet.MessageRow row in messages.GetChanges(DataRowState.Added))
{
foreach (SQLiteParameter parameter in insert.Parameters)
{
parameter.Value = row[parameter.SourceColumn];
}
// use the name of your rowid column
row.ID = Convert.ToInt32(insert.ExecuteScalar());
row.AcceptChanges();
}
}
// then you can perfom the other updates
messagesAdapter.Update(messages);
Note: Be sure to open / close your connection