We migrated from oracle c12 to c19. And now we have simple test case that fails:
//arrange
string data = new string('x', 5000);
var connection = tm.GetConnection();
var createTableCmd = connection.CreateCommand(false);
createTableCmd.Text = "CREATE TABLE xx_temp (id NUMBER, text_long NCLOB, text_short NVARCHAR2(2000))";
createTableCmd.ExecuteNonQuery();
//act
var insertCmd = connection.CreateCommand(false);
insertCmd.Text = "INSERT INTO XX_TEMP (text_long) VALUES (#p1)";
var param = new OracleParameter("#p1", OracleDbType.NClob, data.Length, System.Data.ParameterDirection.Input);
param.Value = data;
insertCmd.Parameters.Add(param);
var insertResult = insertCmd.ExecuteNonQuery();
The last line fails with the error: ORA-64219: invalid LOB locator encountered
We're using Devart.Data.Oracle component. And I have no idea what is wrong.
What is interesting is that when I'm trying to connect to DB from powershell using Oracle.ManagedDataAccess.dll, I have no problems using similar code.
Please upgrade to dotConnect for Oracle v9.11.980. The following code works with v9.11.980 and Oracle 19c:
//arrange
string data = new string('x', 5000);
var connection = new OracleConnection();
connection.ConnectionString = #"User Id=****;Password=****;Server=YOUR_HOST/YOUR_SID;Direct=true;";
connection.Open();
var createTableCmd = connection.CreateCommand();
//createTableCmd.CommandText = "DROP TABLE xx_temp";
//createTableCmd.ExecuteNonQuery();
createTableCmd.CommandText = "CREATE TABLE xx_temp (id NUMBER, text_long NCLOB, text_short NVARCHAR2(2000))";
createTableCmd.ExecuteNonQuery();
//act
var insertCmd = connection.CreateCommand();
insertCmd.CommandText = "INSERT INTO XX_TEMP (text_long) VALUES (:p1)";
var param = new OracleParameter("p1", OracleDbType.NClob, data.Length, System.Data.ParameterDirection.Input);
param.Value = data;
insertCmd.Parameters.Add(param);
var insertResult = insertCmd.ExecuteNonQuery();
Related
I am trying to collect data from a text file in order to populate a table in Sql Server. The collection part is working but at "ExecuteNonQuery" I get the "No mapping exists from object type System.Collections.Generic.List" error. I've researched this error here and at other sites and from what I can gather, the error is caused by trying to populate a table with an array or there is something wrong with my parameters. I've tried various adjustments to the code which populates the table. Haven't touch the collection code as I hate to fix what isn't broken. Here is my code:
var mbrNbr = new List<string>();
var sPages = new List<int>();
var ePages = new List<int>();
var startPage = 0;
var endPage = 0;
using (var sr = new StreamReader(#"\\path\textFile.txt"))
{
while (sr.Peek() > 0)
{
var line = sr.ReadLine();
var memberNumber = line.Substring(698,11);
var passThru = line.Substring(698,11);
var oceMax = Convert.ToInt32(line.Substring(910, 3));
if (passThru.Equals(memberNumber))
{
mbrNbr.Add(memberNumber);
startPage = endPage + 1;
endPage += oceMax * 2;
ePages.Add(endPage);
sPages.Add(startPage);
}
}
}
var sqlConn = new SqlConnection("server/db conneciton");
sqlConn.Open();
var updateSql = #"update myTable
set MemberNbr = #memberNumber,
StartPage = #startPage, EndPage = #endPage";
using (SqlCommand cmd = new SqlCommand(updateSql, sqlConn))
{
cmd.CommandTimeout = 6000;
cmd.Parameters.AddWithValue("#memberNumber", mbrNbr);
cmd.Parameters.AddWithValue("#startPage", sPages);
cmd.Parameters.AddWithValue("#endPage", ePages);
cmd.ExecuteNonQuery();
}
sqlConn.Close();
In an effort to construct a specific question, here goes: Does SQL Server not accept an array or is it a parameter issue or both?
I have a stored procedure which has 2 input parameters and returns multiple result set.
ALTER PROCEDURE [dbo].[sp_GetList]
#eid int,
#sid int,
AS
SELECT ID, NAME FROM EMPLOYEE WHERE id=#eid;
SELECT ID, NAME FROM STUDENTS WHERE id=#sid
In entity framework I am calling this stored procedure as below.
I am using this tutorial from msdn.
https://msdn.microsoft.com/en-us/library/jj691402(v=vs.113).aspx
using (var db = new APIContext()) {
db.Database.Initialize(force: false);
var cmd = db.Database.Connection.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "[dbo].[sp_GetList]";
DbParameter eid = cmd.CreateParameter();
eid.ParameterName = "#eid";
eid.DbType = System.Data.DbType.Int32;
eid.Direction = ParameterDirection.Input;
eid.Value = resourceID;
DbParameter sid = cmd.CreateParameter();
sid.ParameterName = "#sid";
sid.DbType = System.Data.DbType.Date;
sid.Direction = ParameterDirection.Input;
sid.Value = sid;
cmd.Parameters.Add(sid);
try
{
db.Database.Connection.Open();
var reader = cmd.ExecuteReader();
EmpList = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Shared.Model.EmpList>(reader).ToList();
MList.EmpList = EmpList;
reader.NextResult();
SList = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Shared.Model.SList>(reader).ToList();
MList.SList = SList;
}
finally
{
db.Database.Connection.Close();
}
When I run this code, I get error:
Additional information: Procedure or function 'sp_GetList' expects parameter '#eid', which was not supplied.
I understand this is very basic error when it comes to ado.net, but this is different using Entity framework.
Any help is apreciated.
Your code seems to be missing:
cmd.Parameters.Add(eid);
I'm currently using Fluent NHibernate ORM and trying to call oracle function fu_GetUserGrant. I have tried the code below:
var dbCommand = session.Connection.CreateCommand();
dbCommand.CommandText = "select fu_GetUserGrant(#grantId) from dual;";
dbCommand.CommandType = CommandType.Text;
var param = dbCommand.CreateParameter();
{
param.Value = grantId;
param.DbType = DbType.StringFixedLength;
param.Size = 200;
param.ParameterName = "#grantId";
}
dbCommand.Parameters.Add(param);
var result = dbCommand.ExecuteNonQuery();
return long.Parse(result.ToString());
And getting exception - Oracle.ManagedDataAccess.Client.OracleException : ORA-00936: missing expression
After several hours of failure I tried another approach:
var c = session.
CreateQuery("select fu_GetUserGrant(:grantId) from dual;")
.SetParameter("grantId", grantId).UniqueResult<int>();
and getting exception - NHibernate.Hql.Ast.ANTLR.QuerySyntaxException : dual is not mapped [select fu_GetUserGrant(:grantId) from dual;]
any ideas guys? When i retrieve same function from MSSQL it works fine (of course I use different sql query because of MSSQL.)
At first sample you are using SQL query and in second HQL query. In HQL query there is no dual of course. So what you should do is in SQL using ":" instead "#"
dbCommand.CommandText = "select fu_GetUserGrant(:grantId) from dual;"
So I managed to find answer to my problem.
To call oracle function i used code bellow:
var dbCommand = session.Connection.CreateCommand();
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.CommandText = "fu_GetUserGrant";
var returnValue = dbCommand.CreateParameter();
{
returnValue.ParameterName = "Return_Value";
returnValue.DbType = DbType.Decimal;
returnValue.Direction = ParameterDirection.ReturnValue;
}
var grantIdParam = dbCommand.CreateParameter();
{
grantIdParam.Value = grantId;
grantIdParam.DbType = DbType.StringFixedLength;
grantIdParam.Size = 200;
grantIdParam.ParameterName = "m_In_GrantId";
grantIdParam.Direction = ParameterDirection.Input;
}
dbCommand.Parameters.Add(returnValue);
dbCommand.Parameters.Add(grantIdParam);
dbCommand.ExecuteReader();
return long.Parse(returnValue.Value.ToString());
What i learned is that, you can call oracle functions through CommandType.StoredProcedure; if you specify where will be put return value from the function, in this case was - var returnValue ...
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");
My code mainly reads some data from a file and save it on mySql database. I used the insert sql query then used a select sql query to read all data in my database.
When running this code i get the error "Key cannot be null.\r\nParameter name: key} System.Exception {System.ArgumentNullException} "
how do i fix this?
private void bindGrid()//function to connect to database
{
string DocID = User.Identity.Name.ToString();
string connectionString = "server=127.0.0.1;"
+ "database=hospital;"
+ "uid=root;"
+ "pwd=missy3asoola;";
MySqlConnection conn = new MySqlConnection(connectionString);
try
{
conn.Open();
StreamReader objReader = new StreamReader("D:/Senior/WebApplication-metbhdelaXD/WebApplication2/Textfile.txt");
string sLine = "";
ArrayList arrText = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
MySqlCommand cmdread = new MySqlCommand();
cmdread.Connection = conn;
//Defining Mysql Parameters
MySqlParameter Temperature, Pulserate, Timestamp, Doctor_idDoctor, Personal_PatientID;
Temperature = new MySqlParameter();
Pulserate = new MySqlParameter();
Timestamp = new MySqlParameter();
Doctor_idDoctor = new MySqlParameter();
Personal_PatientID = new MySqlParameter();
//Defining Types
Temperature.MySqlDbType = MySqlDbType.Int32;
Pulserate.MySqlDbType = MySqlDbType.Int32;
Timestamp.MySqlDbType = MySqlDbType.Timestamp;
Doctor_idDoctor.MySqlDbType = MySqlDbType.Int32;
Personal_PatientID.MySqlDbType = MySqlDbType.Int32;
//Defining values
Temperature.Value = arrText[0];
Pulserate.Value = arrText[1];
Timestamp.Value = arrText[2];
Doctor_idDoctor.Value = arrText[3];
Personal_PatientID.Value = arrText[4];
//cmdread.Parameters.Clear();
//Adding parameters
cmdread.Parameters.Add(Temperature);
cmdread.Parameters.Add(Pulserate);
cmdread.Parameters.Add(Timestamp);
cmdread.Parameters.Add(Doctor_idDoctor);
cmdread.Parameters.Add(Personal_PatientID);
cmdread.Connection = conn;
cmdread.CommandText = "INSERT INTO medical(Temperature,`Pulse rate`,Timestamp,Doctor_idDoctor,Personal_PatientID)VALUES([#Temperature],[#`Pulse rate`],[#Timestamp],[#Doctor_idDoctor],[#Personal_PatientID]);";
cmdread.CommandType = CommandType.Text;
cmdread.ExecuteNonQuery();
cmdread.Dispose();
string sqlQuery1 = "Select * from medical where Doctor_idDoctor=" + DocID;
MySqlCommand cmdmedical = new MySqlCommand(sqlQuery1, conn);
GridView1.DataSource = cmdmedical.ExecuteReader() ; //ex reader for retrieving
GridView1.DataBind();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex.StackTrace);
}
}
You are not applying the values from the text file to your parameters. If you don't do this, and your column is a non-nullable column, then you will get an exception, and thats exactly what is happening here.
You should change it to be:
cmdread.Parameters.Add("#Temperature",MySqlDbType.Int32, 5).Value = //value from text file;
cmdread.Parameters.Add("#Pulse rate", MySqlDbType.Int32, 3).Value = //value from text file;
cmdread.Parameters.Add("#Timestamp", MySqlDbType.Timestamp, 15).Value = //value from text file;
cmdread.Parameters.Add("#Doctor_idDoctor", MySqlDbType.Int32, 3).Value = //value from text file;
cmdread.Parameters.Add("#Personal_PatientID", MySqlDbType.Int32, 3).Value = //value from text file;
Also, try not to use spaces in your Column names and parameter names. I would change #Pulse rate to #PulseRate
Furthermore I don't understand why you are giving the return value of cmdread.ExecuteNonQuery() as the DataSource to the GridView. This doesn't seem right.