I have the following code in a dll that I use to query a SQL Server 2008 database
connectionString = #"Server=totmobdb-bod\live_mobile_data;Database=TM5Admin;User Id=ourusername;Password=ourpassword;MultipleActiveResultSets=true;";
dbConn.ReturnSingleTable("select Detail from dbo.Details where Name = 'RepairsReport'", mainConnectionString).Rows[0].ItemArray[0].ToString();
public DataTable ReturnSingleTable(string query, string connectionString)
{
DataSet dataSet = new DataSet();
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlDataAdapter adapter = new SqlDataAdapter())
{
adapter.SelectCommand = new SqlCommand(query, conn);
adapter.Fill(dataSet);
};
};
DataTable dataTable = dataSet.Tables[0];
return dataTable;
}
I recently changed a value in the database through SQL Server Management Studio.
The problem is that the query in my code is some times bringing back the old value and sometimes bringing back the new value.
When I connect to the database using the same username/password as the query does I can see the correct value.
Any ideas folks?
Although SQL Server Management Studio has implicit commit turned on,There might be a chance that changes which you made through your update query might not have committed data in the table,use commit after you execute the query.
Related
I need to retrieve the list the tables in an Oracle database that is defined by a DSN that is using the Oracle ODBC driver.
However, OdbcConnection.GetSchema("Tables") throws an exception ERROR [HYT00] [Oracle][ODBC][Ora]ORA-01013: user requested cancel of current operation\n or ORA-00604: error occurred at recursive SQL level 1 after about 30 seconds.
using (OdbcConnection connection = new OdbcConnection("Driver={Oracle in OraDB18Home1};Dbq=XE;Uid=system;Pwd=mypassword;"))
{
connection.Open();
//Also unsuccessful with "Views" and "Columns", but works with "DataTypes" and "Restrictions"
DataTable schema = connection.GetSchema("Tables");
}
The database is newly installed and is not too big.
I can call GetSchema() without parameters to successfully retrieve all supported schema collections.
I can also successfully run a query against my database:
OdbcCommand command = new OdbcCommand("SELECT * FROM vendors")
{
Connection = connection
};
OdbcDataReader reader = command.ExecuteReader();
You should stop using ODBC. Use ODP.NET - this is gold standard Oracle .NET provider. And use "Managed" version, i.e. Oracle.ManangedDataAccess. This code below will work fine
var conn = new OracleConnection("Data Source=server:1521/sid;password=pwd;user id=usr");
conn.Open();
var tbl = conn.GetSchema();
conn.Close();
Consile.WriteLine(tbl.Rows.Count.ToString());
The connection to database is working if I am using SqlDataSource object but it's not working when I am using C# code.
See the error:
http://i.stack.imgur.com/FTLvn.jpg
The problem is the data set returning null while i can track the query on SQL Server profiler.
and i test the query on SQL query editor it returning a result can you told me what the wrong on the code:
Code to return result on Data Set
Code Sample:
static public DataSet GetData(string SqlStatement)
{
DataSet datasetdata=new DataSet();
using (SqlConnection connection =
new SqlConnection(ConfigurationManager.ConnectionStrings["Undp_PortalConnectionString1"].ToString()))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(SqlStatement, connection);
adapter.Fill(datasetdata);
return datasetdata;
}
}
SqlStatement = Select * From Portal_PersonalInfo Where email = 'waleed.obyed#undp.org'
Table :
http://i.stack.imgur.com/jQMnX.jpg
You need to check this while the connection is OPEN - by default, the SqlDataAdapter will open and close the connection as needed - you never get a chance to check while it's open.
If you open the connection yourself
connection.Open();
before the call to .Fill() - then you will see the ServerVersion is quite nicely filled on the connection property:
I found the problem from the DataSet file on the App_Code called DataSet.xsd this file add to datasetdata Table called ES_ICTCR see the photo, i delete this file i think because it has common name DataSet.xsd.
check the Table Count= 2.
http://i.stack.imgur.com/f66JJ.jpg
Im simply just trying to read what there is in the batabase on to a console but i always get an exception on the conn.Open() line. Here is all the code:
SqlConnectionStringBuilder conn_string = new SqlConnectionStringBuilder();
conn_string.DataSource = "mysql14.000webhost.com"; // Server
conn_string.UserID = "a7709578_codecal";
conn_string.Password = "xxxxx";
conn_string.InitialCatalog = "a7709578_codecal"; // Database name
SqlConnection conn = new SqlConnection(conn_string.ToString());
conn.Open();
SqlCommand cmd = new SqlCommand("Select name FROM Users");
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{1}, {0}", reader.GetString(0), reader.GetString(1));
}
reader.Close();
conn.Close();
if (Debugger.IsAttached)
{
Console.ReadLine();
}
You need to build the connection string manually or use MySqlConnectionStringBuilder. MySql uses a different format than SQL Server and the SqlConnectionStringBuilder that you're using. You also need to use a MySQL library, SqlConnection, SqlCommand, etc are all build specifically for SQL Server.
MySQL connectors
For MySQL database you are using wrong provider. Those classes you have used in posted code are for SQL Server. Your code should look like below with MySQL provider related classes
MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "mysql14.000webhost.com";
conn_string.UserID = "a7709578_codecal";
conn_string.Password = "xxxxxxx";
conn_string.Database = "a7709578_codecal";
using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
Check Related post in SO
Also to point out, you are selecting only one column from your table as can be seen
new SqlCommand("Select name FROM Users");
Whereas trying to retrieve two column value, which is not correct
Console.WriteLine("{1}, {0}", reader.GetString(0), reader.GetString(1))
000webhost free servers does not allow external connections to the server database.
You can only use your database from your PHP scripts stored on the server.
You can get data from database using PHP and it will return.So i advice to you using php from C# like api.
I have a SQL CLR trigger written in C# 4.0 and deployed on SQL Server 2014. Whenever an insertion happens in a table in SQL Server, this CLR trigger's job is to import that row in an Oracle database. So basically I have to import data in Oracle database whenever an insert query is fired on a table in SQL Server 2014. This is my first CLR SQL trigger project and below is what I am doing:
[SecurityCritical]
[OraclePermission(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]
[SqlTrigger(Name = "FetchSurvey", Target = "temp", Event = "FOR INSERT")]
public static void FetchSurvey()
{
SqlTriggerContext triggerContext = SqlContext.TriggerContext;
// Create result set to store data
DataSet resultSet = new DataSet();
// Create a new SQL command
using (SqlCommand command = new SqlCommand("SELECT * FROM INSERTED"))
{
// Create a new SQL connection
using (command.Connection = new SqlConnection("context connection=true"))
{
// Connect to the database
command.Connection.Open();
// Execute procedure
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
adapter.Fill(resultSet);
}
// Disconnect from the database
command.Connection.Close();
}
}
SqlPipe sqlP = SqlContext.Pipe;
// Return data
if (resultSet.Tables.Count > 0)
SaveSurvey(resultSet);
sqlP.Send("Finaly its done!!");
}
public static void SaveSurvey(DataSet dsSurvey)
{
using (OracleConnection con = new OracleConnection("my oracle connection string"))
{
if (con.State == ConnectionState.Closed)
con.Open();
DataRowView drv = dsSurvey.Tables[0].DefaultView[0];
using (OracleCommand cmd = new OracleCommand("AddMetaData", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("V_id", drv["TemplateID"]);
cmd.Parameters.AddWithValue("V_Title", drv["TemplateName"]);
cmd.Parameters.AddWithValue("V_CreatedBy", drv["CreatedBy"]);
cmd.Parameters.AddWithValue("V_IsActive", drv["IsActive"]);
cmd.ExecuteNonQuery();
}
}
}
And this is my code to create assembly/deploy trigger:
CREATE ASSEMBLY TriggerImportSurvey
FROM 'C:\ImportSurvey\SQL-CLR-Trigger.dll'
With Permission_Set = External_Access;
Now the problem is whenever I run an insert query in SQL Server to insert data, I got below error in SQL Server:
Msg 6522, Level 16, State 1, Procedure tri_InsertSurvey_clr, Line 18
A .NET Framework error occurred during execution of user-defined routine or aggregate "tri_InsertSurvey_clr":
System.InvalidOperationException: Cannot perform CAS Asserts in Security Transparent methods
System.InvalidOperationException:
at Triggers.FetchSurvey()
tri_InsertSurvey_clr is the trigger which is responsible for executing the assembly whenever I run an insert statement.
Please tell me what I am missing so that I am getting this error, Also if there a more elegant way of implementing a CLR SQL trigger then please also suggest that.
NOTE: When I tried to save the data using a trigger in SQL Server I was successful, but now when I am trying to save it in Oracle database, I am getting this error. Also the Oracle database is installed on another machine.
I am new to Oracle. There is a requirement in our firm for a project using Oracle and .Net. So I was trying to run a demo application. I am using Oracle 10g XE as DB and VS2010.
I wrote a procedure with a simple select query, which got compiled (got this procedure format by googling).
I ran the stored procedure from the SQL command prompt from XE Dashboard itself. This was the result:
Now I wrote code in C# to call that stored procedure:
string sqlCon = "Data Source=xe;Persist Security Info=True;User ID=sa;Password=password;Unicode=True;Provider=OraOLEDB.Oracle;";
OleDbConnection Con = new OleDbConnection(sqlCon);
OleDbCommand cmd = new OleDbCommand();
DataSet ds = null;
OleDbDataAdapter adapter;
try
{
Con.Open();
////Stored procedure calling. It is already in sample db.
cmd.CommandText = "TESTPROC";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Con;
ds = new DataSet();
adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds, "Users");
return ds.Tables[0];
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
This code block didn't throw any exceptions. It got executed but what I received was an empty Dataset. But when I tried to fetch data using the query directly I got the result.
So, is the way I am trying to access the stored procedure correct? or Is there any mistake in my stored procedure? Can anyone point out the error or the best way to do this?
Thanks in advance.
Suggestion: try declaring "result" as an "out" parameter:
https://forums.oracle.com/forums/thread.jspa?messageID=4566433
Here's one other link that might help:
http://support.microsoft.com/kb/309361
There is another link that might help:
http://www.akadia.com/services/ora_return_result_set.html