selecting non-english character using oracle client in c# - c#

I'm trying execute query in C# to store the data in DataTable. however the database has some non-English character(AR8MSWIN1256). the result of the select query returns (????)
my code :
System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection();
System.Data.OracleClient.OracleConnectionStringBuilder ocsb = new System.Data.OracleClient.OracleConnectionStringBuilder();
ocsb.Password = "password";
ocsb.UserID = "username";
ocsb.DataSource = "datasource";
ocsb.Unicode = true;
System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand(
"SELECT 'ب' LETTER FROM DUAL", con);
Console.WriteLine(cmd.ExecuteScalar().ToString());
the result is (?)
can anyone help with this issue. thank you

Related

cannot apply indexing with to an expression of type 'system.data.datatable'

I have a Login table in SQL Server 2008 and I want to check for a valid user in DataColumn.
I was trying to retrieve value from DataColumn by indexing, but got the error..
cannot apply indexing with to an expression of type 'system.data.datatable'.
Here is the code:
string connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=DRZare;Integrated Security=true;";
SqlConnection LOGINCONNECTION = new SqlConnection(connectionString);
string commandText = "select * from Login where UserName = #User and Password = #Pass";
SqlCommand cmdlogin = new SqlCommand(commandText, LOGINCONNECTION);
cmdlogin.Parameters.AddWithValue("#User", TextBox5.Text);
cmdlogin.Parameters.AddWithValue("#Pass",TextBox6.Text);
LOGINCONNECTION.Open();
DataTable logintable = new DataTable();
logintable.Load(cmdlogin.ExecuteReader());
for (int i = 0; i < logintable.Rows.Count; i++ )
{
User = Convert.ToString(logintable[i]["UserName"]);
string Pass = Convert.ToString(logintable[i]["Password"]);
}
Help me out.
Error message says clearly where is the problem. You can't use indexer on DataTable. But you can use it with DataRow.
Change to:
foreach (DataRow row in loginTable.Rows )
{
string User = Convert.ToString(row["UserName"]);
string Pass = Convert.ToString(row["Password"]);
}
Thanks for your help. The code was changed into this and now it works correctly.
string User = Convert.ToString(logintable.Rows[i]["UserName"]);
string Pass = Convert.ToString(logintable.Rows[i]["Password"]

Data Type Mismatch error in Criteria expression in Select query C# query

My sample code is as follows, I am getting following error;
Data Type Mismatch error in criteria expression.
Details => ScannerAlarmLimits is my table from .mdb database.
string jointS = dsetChamberS1.Tables[0].Rows[tot][0].ToString();
int select1S = Convert.ToInt32(jointS);
string sqlQuery1S = "SELECT TMin,TMax,HMin,HMax from ScannerAlarmLimits WHERE ScannerID='" +select1S+ "'";
OleDbCommand cmd1S = new OleDbCommand(sqlQuery1S, conn);
OleDbDataAdapter adapter1S = new OleDbDataAdapter(cmd1S);
adapter1S.Fill(dsetTempS, "ScannerAlarmLimits");
I just added single quote in the condition of where clause, now its working.
var query = "SELECT * from checkinout where read <> '1'";
If your ScannerID column is integer, then you should not use single quotes with it. Single quotes are for characters. Like;
WHERE ScannerID = " + select1S;
But as a better way, you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks. Aka bobby-tables.
And use using statement to dispose your connections, commands and adapters.
string jointS = dsetChamberS1.Tables[0].Rows[tot][0].ToString();
int select1S = Convert.ToInt32(jointS);
using(var conn = new OleDbConnection(conString))
using(var cmd1S = conn.CreateCommand())
{
cmd1S.CommandText = "SELECT TMin,TMax,HMin,HMax from ScannerAlarmLimits WHERE ScannerID = #id";
cmd1S.Parameters.AddWithValue("#id", OleDbType.Integer).Value = select1S;
using(var adapter1S = new OleDbDataAdapter(cmd1S))
{
adapter1S.Fill(dsetTempS, "ScannerAlarmLimits");
}
}

insert command syntax error c#

I'm trying to write into an Access db. The OLE DB connection is stored on the main form (mainFrm). I read and write using the same connection in other parts of my app. For some reason, at this one spot it says I have a syntax error. I have tried writing it different ways but still get the same error message
public OleDbConnection newCon = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MorganWillis\Documents\PlannerAppData\MainDB.accdb");`
ListViewItem newnote = new ListViewItem(nameTextBox.Text);
newnote.SubItems.Add(DateTime.Today.ToShortDateString());
newnote.SubItems.Add(noteTextBox.Text)`
mainFrm.notesList.Items.Add(newnote);
string tempname = nameTextBox.Text;
DateTime now = DateTime.Today;
string tempnote = noteTextBox.Text;
if(mainFrm.newCon.State == ConnectionState.Closed)
mainFrm.newCon.Open();
OleDbCommand noteCom = new OleDbCommand("INSERT INTO Notes (noteName, noteDate, note)" + "VALUES (?,?,?)", mainFrm.newCon);
noteCom.Parameters.Add("noteName", OleDbType.Char, 50, "noteName").Value = tempname;
noteCom.Parameters.Add("noteDate", OleDbType.DBDate, 8, "noteDate").Value = now.ToShortDateString();
noteCom.Parameters.Add("note", OleDbType.Char, 1000, "note").Value = tempnote;
OleDbDataAdapter noteadapt = new OleDbDataAdapter();
noteadapt.InsertCommand = noteCom;
noteadapt.InsertCommand.ExecuteNonQuery();
mainFrm.newCon.Close();
Close();
You have an errant + in the middle of your query.
It must be eliminated.
Notes was is a key word in access so it wouldn't let me use it for a table name but when I changed the table name it worked just fine Than you everyone for your help!

Pervasive SQL System Stored Procedure from ADO.NET error

I'm just trying to return a list of columns and their attributes through a system stored procedure. What documentation I have seems to say the below code should work, but I get "Pervasive.Data.SqlClient.Lna.k: [LNA][Pervasive][ODBC Engine Interface]Invalid or missing argument." on the execute. This is PSQL v11, .NET 4.5.
using (PsqlConnection conn = new PsqlConnection(cs))
{
PsqlCommand locationCmd = new PsqlCommand();
PsqlParameter tableParam = new PsqlParameter();
PsqlParameter returnParam = new PsqlParameter();
returnParam.Direction = ParameterDirection.ReturnValue;
locationCmd.CommandText = "psp_columns";
locationCmd.Connection = conn;
locationCmd.CommandType = CommandType.StoredProcedure;
locationCmd.Parameters.Add(tableParam).Value = table;
locationCmd.Parameters.Add(returnParam);
conn.Open();
locationCmd.ExecuteNonQuery();
}
I would think the problem is this line:
locationCmd.Parameters.Add(tableParam).Value = table;
You should set the value before adding the parameter, not afterwards.
tableParam.Value = table;
locationCmd.Parameters.Add(tableParam);
I don't know about Psql but for MSSQL normally you also need to define the parameter name as its found in the stored procedure, or at least that's what I do.
SqlParameter param = new SqlParameter("#tableParam", value);
The psp_Columns system stored procedure is defined as call psp_columns(['database_qualifier'],'table_name', ['column_name']). I know that it says the database qualifier is optional, but I think it's required. You could try passing an empty string for the qualifier. Something like:
using (PsqlConnection conn = new PsqlConnection(cs))
{
PsqlCommand locationCmd = new PsqlCommand();
PsqlParameter dbParam = new PsqlParameter();
PsqlParameter tableParam = new PsqlParameter();
PsqlParameter returnParam = new PsqlParameter();
returnParam.Direction = ParameterDirection.ReturnValue;
locationCmd.CommandText = "psp_columns";
locationCmd.Connection = conn;
locationCmd.CommandType = CommandType.StoredProcedure;
locationCmd.Parameters.Add(dbParam).Value = ""; //might need two single quotes ('')
locationCmd.Parameters.Add(tableParam).Value = table;
locationCmd.Parameters.Add(returnParam);
conn.Open();
locationCmd.ExecuteNonQuery();
}
You should try to get the information of the table SCHEMA using the provided GetSchema method from the Psqlconnection. I have searched a bit on their support site and it seems that this method is supported although I haven't find a direct example using the Tables collection.
This is just an example adapted from a test on mine on SqlServer, I don't have Pervasive install, but you could try if the results are the same
using(PsqlConnection cn = new PsqlConnection("your connection string here"))
{
cn.Open();
string[] selection = new string[] { null, null, table };
DataTable tbl = cn.GetSchema("Columns", selection);
foreach (DataRow row in tbl.Rows)
{
Console.WriteLine(row["COLUMN_NAME"].ToString() + " " +
row["IS_NULLABLE"].ToString() + " " +
row["DATA_TYPE"].ToString()
);
}
}
i was trying to figure this out as well, but with the tables procedure. even though the database and table names are optional, you still have to provide values. for optional parameters, pass in DBNull.Value
this worked for me:
PsqlCommand cm = new PsqlCommand();
cm.CommandText = "psp_tables";
cm.CommandType = CommandType.StoredProcedure;
cm.Connection = new PsqlConnection();
cm.Connection.ConnectionString = <your connection string>;
cm.Parameters.Add(":database_qualifier", DBNull.Value);
cm.Parameters.Add(":table_name", DBNull.Value);
cm.Parameters.Add(":table_type", "User table");

Search a string that contains special character in MS Access

I have problem searching for a long string that contains special character in MS Access. Here is my sample data.
staff_Id | hashValue
1 | 4ENOA2838F09dbfTKXeAdEIKRM91MdsDg0W4pRNChdkGa7iwoVifWH9avZdjrPp1QqLJ0ecNe/X716HlwqfSYA==
Here is my SQL command.
SELECT *
FROM table
WHERE hashValue='4ENOA2838F09dbfTKXeAdEIKRM91MdsDg0W4pRNChdkGa7iwoVifWH9avZdjrPp1QqLJ0ecNe/X716HlwqfSYA==';
I had tried googling for escape characters, however I cannot get this working. Hope that you can help me. Thank you.
P.S. I am developing a C# program that interacts with MS-access
UPDATE
Here is my SQL query in my C# program that execute the search query.
string sqlStatement = "SELECT * FROM table WHERE hashValue = #hashedValue";
using (OleDbConnection connection = new OleDbConnection(connString))
{
using (OleDbCommand command = new OleDbCommand())
{
command.Connection = connection;
command.CommandText = sqlStatement;
command.Parameters.AddWithValue("#hashedValue", hashedValue);
ds = new DataSet(); //have been declared
dbAdapter = new OleDbDataAdapter(); //have been declared
dbAdapter.SelectCommand = command;
dbAdapter.Fill(ds, "table"); //empty dataset here
}
}
in C# try writing the command like this:
string hashValue = #"4ENOA2838F09dbfTKXeAdEIKRM91MdsDg0W4pRNChdkGa7iwoVifWH9avZdjrPp1QqLJ0ecNe/X716HlwqfSYA==";
string sql = string.Format("SELECT * FROM table WHERE hashValue = '{0}'", hashValue);
notis the # mark before the string. this allows you to have string that look like this:
string test = #" \ ";
Console.WriteLine(test); //output: \
In your case you can do it like this:
command.Parameters.AddWithValue("#hashedValue", #hashedValue);
but the #-trick works best if its set when the string is declared.
Why not write a query using wildcard?
SELECT *
FROM table
WHERE hashValue Like "*4ENOA2838F09dbfTKXeAdEIKRM91MdsDg0W4pRNChdkGa7iwoVifWH9avZdjrPp1QqLJ0ecNe/X716HlwqfSYA==*";

Categories