And I solved this problem in VBA using DAO here
Using the Oledb framework, I created a function that can look up a record value. However it only gets the raw value. I need to find the value of the column property called "Row Source"
Can someone explain to me how to find the foreign key value using Oledb
Below is my function for a traditional look up query.
string IDatabase.LookupRecord(string column, string table, string lookupColumn, string lookUpValue)
{
OleDbCommand cmdLookupColumnValue = new OleDbCommand();
string sqlQuery = "SELECT [" + table + "].[" + column + "] " +
"FROM [" + table + "] " +
"WHERE [" + table + "].[" + lookupColumn + "] = '" + lookUpValue + "'";
cmdLookupColumnValue.CommandText = sqlQuery;
cmdLookupColumnValue.CommandType = CommandType.Text;
cmdLookupColumnValue.Connection = connection;
string result = "";
try
{
result = cmdLookupColumnValue.ExecuteScalar().ToString();
}
catch(Exception ex)
{
MessageBox.Show("Query is not valid :" + ex.ToString());
}
return result;
}
EDIT I found the following code here Its the closest Ive gotten so far. It does get column properties like the column Description but it doesnt work for row Source. Any Ideas?
public void Test()
{
string columnName = "Main Space Category";
ADOX.Catalog cat = new ADOX.Catalog();
ADODB.Connection conn = new ADODB.Connection();
conn.Open(connectionString, null, null, 0);
cat.ActiveConnection = conn;
ADOX.Table mhs = cat.Tables["FI - ROOM"];
var columnDescription = mhs.Columns[columnName].Properties["Description"].Value.ToString();
MessageBox.Show(columnDescription);
conn.Close();
}
I strongly suspect that the RowSource property of a table column is so specific to Access that you will have to use DAO to retrieve it. The following C# code is an example of how you might do that:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace daoConsoleApp
{
class Program
{
static void Main(string[] args)
{
string TableName = "Cars";
string FieldName = "CarType";
// This code requires the following COM reference in your project:
//
// Microsoft Office 14.0 Access Database Engine Object Library
//
var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine();
Microsoft.Office.Interop.Access.Dao.Database db = dbe.OpenDatabase(#"Z:\_xfer\Database1.accdb");
try
{
Microsoft.Office.Interop.Access.Dao.Field fld = db.TableDefs[TableName].Fields[FieldName];
string RowSource = "";
try
{
RowSource = fld.Properties["RowSource"].Value;
}
catch
{
// do nothing - RowSource will remain an empty string
}
if (RowSource.Length == 0)
{
Console.WriteLine("The field is not a lookup field.");
}
else
{
Console.WriteLine(RowSource);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
Related
Hello i'm using linq and sql to create a windows serrvice that updates some rows if there was a change in the table in the last 5 minutes so far this is what i have:
using System;
using System.Data.SqlClient;
using System.Linq;
namespace Prueba
{
internal static class Program
{
private static void Main()
{
int tienda = 9;
var conex = new DataClasses1DataContext();
try
{
var source =
new SqlConnection(
"Server=LAPTOP-VCD9V9KH\\SQLEXPRESS;Database=backoffice;User Id=sa; Password=root;");
var destination =
new SqlConnection(
"Server=LAPTOP-VCD9V9KH\\SQLEXPRESS;Database=Corporativo_PRB;User Id=sa; Password=root;");
source.Open();
destination.Open();
source.CreateCommand();
var infoExistenciases = conex.Info_Existencias.Where(x => x.FechAct > DateTime.Now.AddMinutes(-5));
foreach (var x in infoExistenciases)
{
var cmd2 = new SqlCommand(
"update Info_Corp_Existencias set Existencia =" + x.Existencia + " where sku ='" + x.SKU + "'" +
"AND Tienda =" + tienda, destination);
cmd2.ExecuteNonQuery();
}
source.Close();
destination.Close();
Console.Beep();
}
catch
(Exception e)
{
Console.WriteLine(e);
Console.ReadLine();
throw;
}
}
}
}
So far this code updates everything in the destination server but i can't really really pinpoint down how to put everything into a temp table to let linq update it foreach argument.
Thank you for your time
Since it's in two different tables, it's difficult to select the corresponding row in the second table without knowing the table design exactly. But you can probably do something like this:
var cmd2 = new SqlCommand(
"update Info_Corp_Existencias set Existencia = (select field FROM Info_Existencias WHERE id=#otherid where sku ='" + x.SKU + "'" +
"AND Tienda =" + tienda, destination);
cmd2.ExecuteNonQuery();
So leave out the foreach statement and just launch ONE update statement to the database that will update every row's field by using a subselect to get the correct value from the row.
Hope this helps.
I am trying to create a chart that when, at the push of a button displays a chart that shows the user the number of times a record has appeared in the dataset/table that it is linked to. Please bare in mind that I have little experience with using Charts in Visual Studios/C#.
Currently I am getting this error: Error
This is all the code I have so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace RRAS
{
public partial class formRRAS : Form
{
public OleDbConnection DataConnection = new OleDbConnection();
public formRRAS()
{
InitializeComponent();
}
private void formRRAS_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'database1DataSet.tblReject_test' table. You can move, or remove it, as needed.
this.tblReject_testTableAdapter.Fill(this.database1DataSet.tblReject_test);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnSearch_Click(object sender, EventArgs e)
{
//This creates the String Publisher which grabs the information from the combo box on the form.
//Select and Dataconnection are also defined here.
string Select = "SELECT * FROM tblReject_test";
string DataConnection;
string Department = txtDepartment.Text;
string Start_Date = txtStart.Text;
string End_Date = txtEnd.Text;
string Anatomy = txtAnatomy.Text;
string RFR = cmbRFR.Text;
string Comment = txtComment.Text;
//Select defines what should be loaded on to the dataset.
if (Department != "")
{
Select = Select + " WHERE department_id =" + "'" + Department + "'";
if (Anatomy != "")
{
Select = Select + "AND body_part_examined =" + "'" + Anatomy + "'";
if (Start_Date != "")
{
Select = Select + " AND study_date =" + "'" + Start_Date + "'";
if (End_Date != "")
{
Select = Select + " AND study_date =" + "'" + End_Date + "'";
if (RFR != "")
{
Select = Select + " AND reject_category =" + "'" + RFR + "'";
if(Comment != "")
{
Select = Select + " AND reject_comment =" + "'" + Comment + "'";
}
}
}
}
}
}
else
{
Select = "SELECT * FROM tblReject_test";
}
//DataConnection connects to the database.
string connectiontring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Database1.mdb";
DataConnection = new OleDbConnection(connectiontring);
//The DataAdapter is the code that ensures both the data in the Select and DataConnection strings match.
OleDbDataAdapter rdDataAdapter = new OleDbDataAdapter(Select, DataConnection);
try
{
//It then clears the datagridview and loads the data that has been selected from the DataAdapter.
database1DataSet.tblReject_test.Clear();
rdDataAdapter.Fill(this.database1DataSet.tblReject_test);
}
catch (OleDbException exc)
{
System.Windows.Forms.MessageBox.Show(exc.Message);
}
}
private void btnLoadChart_Click(object sender, EventArgs e)
{
try
{
int count = database1DataSet.Tables["tblReject_test"].Rows.Count;
DataConnection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = DataConnection;
string query = "SELECT * FROM tblReject_test";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
charRejections.Series["RFR"].Points.AddXY(reader["reject_category"].ToString(), reader[count].ToString());
}
DataConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
}
}
Your code wouldn't compile as you are assigning a string to DataConnection (instance of OleDbConnection).
The correct usage should be as following.
string connectiontring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Database1.mdb";
DataConnection = new OleDbConnection(connectiontring));
Also, your code doesn't close Database connection in case of exception.
It would be recommended to use the code as shown below. This is taken from MSDN
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
try
{
connection.Open();
Console.WriteLine("DataSource: {0} \nDatabase: {1}",
connection.DataSource, connection.Database);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.
}
I have created an app linked to a local database. It works well, but the only problem is that after I press the insert button, data is inserted in db, but it is not showed in the GridView, only after I close and reopen the application. How can I make it show the data right after I press the button which inserts the values? Thanks !
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;
namespace Gradinita
{
public partial class Grupa : Form
{
string nume = "";
List<Label> labels = new List<Label>();
public Grupa(string nume)
{
InitializeComponent();
this.nume = nume;
}
private void Grupa_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'grupeDataSet8.copii' table. You can move, or remove it, as needed.
this.copiiTableAdapter2.Fill(this.grupeDataSet8.copii);
// TODO: This line of code loads data into the 'grupeDataSet7.copii' table. You can move, or remove it, as needed.
this.copiiTableAdapter1.Fill(this.grupeDataSet7.copii);
// TODO: This line of code loads data into the 'grupeDataSet3.copii' table. You can move, or remove it, as needed.
this.copiiTableAdapter.Fill(this.grupeDataSet3.copii);
var connString = (#"Data Source=" + System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + #"\Grupe.sdf");
using (var conn = new SqlCeConnection(connString))
{
try
{
conn.Open();
var query = "SELECT * FROM grupe WHERE Nume='" + nume + "'";
var command = new SqlCeCommand(query, conn);
var dataAdapter = new SqlCeDataAdapter(command);
var dataTable = new DataTable();
dataAdapter.Fill(dataTable);
label1.Text = dataTable.Rows[0][0].ToString();
label2.Text = dataTable.Rows[0][1].ToString();
label3.Text = dataTable.Rows[0][2].ToString();
label4.Text = dataTable.Rows[0][3].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
label5.Text = ("1");
}
if (checkBox2.Checked)
{
label5.Text = ("0");
}
textBox1.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox6.Text)).ToString();
var connString = (#"Data Source=" + Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + #"\Grupe.sdf");
using (var conn = new SqlCeConnection(connString))
{
try
{
conn.Open();
var query = "INSERT INTO copii(prezenta, Nume, Prenume, Program, Taxa, Achitat, Diferenta) VALUES('" + label5.Text + "', '" + textBox2.Text.Trim() + "', '" + textBox3.Text.Trim() + "', '" + textBox4.Text.Trim() + "', '" + textBox5.Text.Trim() + "', '"+ textBox6.Text.Trim()+"', '"+ textBox1.Text.Trim() +"');";
MessageBox.Show(query);
var command = new SqlCeCommand(query, conn);
command.ExecuteNonQuery();
dataGridView1.Refresh(); //not working obviously
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
You need to re-query the data and rebind the datatable.
Something like:
dataGridView1.DataSource = SomeDataTableSource;
dataGridView1.DataBind();
I managed to bypass this by re-adding the grid to the control. First, you copy the grid in a variable, then you remove it from the parent control, and then you add the variable to the controls of that control.
var grid = dataGridView1.Parent.Controls["dataGridView1"];
var ctr = dataGridView1.Parent;
ctr.Controls.Remove(dataGridView1);
ctr.Controls.Add(grid);
It's not tested and I might have mistaken some names cause i don't have a VS installed here, but you get the idea. Not the most elegant solution, but it worked for me. You can also try dataGridView1.Refresh() - which it didn't work for me.
I have a base class which implements my database connection. I have a second class that inherits this base database class. The second class has some recursion inside of it, where when evaluating it's value, it may instantiate another instance of the second class. The recursion is only a few levels deep. I am running everything single-threaded.
My code will run correctly for about 1 or 2 minutes, then I begin getting consistent errors: "Timeout Expired. The timeout period elapsed prior to obtaining a connection from the pool".
My base class has a destructor which calls the .Dispose() method on the database objects. My second class has a destructor which closes the connection object in the base class.
My connection string to the database specifies a connection timeout = 0.
Any ideas as to why the code will work correctly for a few minutes and then begin timing out trying to connect to the database? I'm baffled.
namespace BaseLib2
{
public class TSBase
{
protected StreamWriter logFile;
protected OleDbCommand queryCmd;
protected OleDbCommand exeCmd;
protected OleDbConnection connection;
protected OleDbDataReader reader;
public SqlConnection sqlconn;//used for BCP
public TSBase()
{
}
~TSBase()
{
try
{
queryCmd.Dispose();
exeCmd.Dispose();
reader.Dispose();
connection.Dispose();
sqlconn.Dispose();
}
catch (Exception ex)
{
Console.WriteLine("BaseLib2 destrutor:" + ex.Message);
}
}
public void ConnectToDB()
{
string connString = "Provider=SQLNCLI11;Server=myserver;Database=mydb;Uid=myid;pwd=password;connection timeout=0";
queryCmd = new OleDbCommand();
exeCmd = new OleDbCommand();
connection = new OleDbConnection(connString);
queryCmd.CommandTimeout = 60000;
exeCmd.CommandTimeout = 60000;
connection.Open();
queryCmd.Connection = connection;
exeCmd.Connection = connection;
string sqlConnString = "server=dc2k8housql;database=mydb;Uid=myid;pwd=password;connection timeout=0";
sqlconn = new SqlConnection(sqlConnString);
sqlconn.Open();
}
public class Expression : BaseLib2.TSBase
{
private string ExpName;
private string ExpressionTxt;
private string sql;
private DateTime Contract_dt;
private DateTime Quote_dt;
private bool SaveToDB;
private string BaseSymbol;
public Expression(string expNameIn, DateTime contract_dtIn, DateTime quote_dtIn)
{
ExpName = expNameIn;
Contract_dt = contract_dtIn;
Quote_dt = quote_dtIn;
try
{
try
{
ConnectToDB();
}
catch (Exception ex)
{
Console.WriteLine("Error in EXP constructor connecting to database." + ex.Message );
throw new Exception("Error in EXP constructor connecting to database.");
}
//get expression text from database
sql = "select expression, save_to_db, coalesce(base_symbol, '') as base_symbol from expressions where exp_name = " + DBI(ExpName);
reader = ReadData(sql);
if (reader.Read())//should only return 1 row
{
ExpressionTxt = reader[0].ToString();
SaveToDB = bool.Parse(reader[1].ToString());
BaseSymbol = reader[2].ToString();
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception in Expression constructor:" + ex.Message);
}
}
~Expression()
{
try
{
connection.Close();
sqlconn.Close();
connection.Dispose();
sqlconn.Dispose();
}
catch (Exception ex)
{
Console.WriteLine("Error in destructor:" + ex.Message);
}
}
public double Eval()
{
try
{
//check to see if there are any $RV in the expression
if (ExpressionTxt.Contains("$RV("))
{
//parse and evaluate the $RV's
String[] split = ExpressionTxt.Split(("$".ToCharArray()));
foreach (string s in split){
Console.WriteLine("s=" + s);
if (s.Length > 3)//make sure we have a string with a symbol in it
{
//for each rv we find, create a new expression and evaluate it
if (s.Substring(0, 3).Contains("RV"))
{
int pStart = s.IndexOf("(");
int pEnd = s.IndexOf(")");
string rvSymb = s.Substring(pStart + 1, pEnd - pStart - 1);
System.Console.WriteLine(rvSymb);
Expression oExp = new Expression(rvSymb, Contract_dt, Quote_dt);
double rVal = oExp.Eval();//recursive call
oExp = null;
ExpressionTxt = ExpressionTxt.Replace("$RV(" + rvSymb + ")", rVal.ToString());
}
}
}
}
//replace SV values in formula
if (ExpressionTxt.Contains("$SV("))
{
//find symbols in $SV brackets and collect contract dates
String[] split = ExpressionTxt.Split (("$".ToCharArray()));
foreach (string s in split)
{
if (s.Length > 3)
{//make sure we have a symbol
if (s.Substring(0, 3).Contains("SV"))
{
int pStart = s.IndexOf("(");
int pEnd = s.IndexOf(")");
string svSymb = s.Substring(pStart + 1, pEnd - pStart - 1);
System.Console.WriteLine("sv=" + svSymb);
//replace $SV with numerical values
double sVal = GetQuoteValue(svSymb);
ExpressionTxt = ExpressionTxt.Replace("$SV(" + svSymb + ")", sVal.ToString());
}
}
}
}
//evaluate
double ret = Evaluate(ExpressionTxt);
Console.WriteLine(ExpName + "=" + ret.ToString());
if (SaveToDB)
{
Console.WriteLine(ExpName + " cd:" + Contract_dt.ToShortDateString() + " qd:" + Quote_dt.ToShortDateString() + ": saving to db...");
sql = "delete from exp_quotes where exp_name = " + DBI(ExpName ) ;
sql = sql + " and contract_dt = " + DBI(Contract_dt.ToShortDateString());
sql = sql + " and quote_dt = " + DBI(Quote_dt.ToShortDateString());
WriteData(sql);
sql = "insert into exp_quotes(exp_name, contract_dt, quote_dt, calculated_dt, price) values(";
sql = sql + DBI(ExpName ) + "," + DBI(Contract_dt.ToShortDateString()) + "," + DBI(Quote_dt.ToShortDateString());
sql = sql + ", getdate(), " + ret + ")";
WriteData(sql);
}
connection.Close();//after we evaluate, close down the connection
connection.Dispose();
return ret;
//return value
}
catch (Exception ex)
{
Console.WriteLine("exp:" + ExpName + " cd:" + Contract_dt.ToShortDateString() + " qd:" + Quote_dt.ToShortDateString() + " = " + ex.Message);
}
return 0;
}
private double GetQuoteValue(string symbIn)
{
double ret = 0;
sql = "select close_val from prices_union_all_vw where symbol = " + DBI(symbIn) + " and contract_dt = " + DBI(Contract_dt.ToShortDateString()) + " and quote_dt = " + DBI(Quote_dt.ToShortDateString());
reader = ReadData(sql);
if (reader.Read())
{
ret = Double.Parse(reader[0].ToString());
reader.Close();
}
else
{//we didn't get a record for the specific quote date, try again using the mostrecent view
sql = "select close_val from prices_union_all_mostrecent_vw where symbol = " + DBI(symbIn) + " and contract_dt = " + DBI(Contract_dt.ToShortDateString());
reader = ReadData(sql);
if (reader.Read())
{
ret = Double.Parse(reader[0].ToString());
}
reader.Close();
}
return ret;
}
private static double Evaluate(string expression)
{
var loDataTable = new DataTable();
var loDataColumn = new DataColumn("Eval", typeof(double), expression);
loDataTable.Columns.Add(loDataColumn);
loDataTable.Rows.Add(0);
return (double)(loDataTable.Rows[0]["Eval"]);
}
You are exhausting your available pool of connections because you are creating a connection to the database for every Expression and sub-Expression that you parse, and they are not being cleaned up in time to be re-used.
Solution: Do not make connections recursively, or iteratively, or whatever. Make one for one purpose and just use it. And if you need to release a connection in-time for you to re-use it, do NOT rely on class destructors, because they do not run when you want them to.
In general, classes that try to allocate limited external resources (like connections) implicitly in their Initializers should be pretty static objects, and you certainly do not normally want to inherit them in a class that is intended to create objects as dynamically as a parser.
Have you tried extending the timeout period?
Add a big timeout to the connection string like "Connect Timeout=1800". This usually helps me when I get such messages.
The other thing you can see is if you can improve the query more.
You might check your Max connection setting for the database. Also check how many active connections are open when new connection attempts start to time out.
How to determine total number of open/active connections in ms sql server 2005
I have a MS Access file for data logging in my C# project but when the system is open and another remote workstation wants to access this MS Access file, it notifies that you cannot access that file because it is already in use? Can this be programmed in C# that this MS Access file can be shared to another workstation?
Here is my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.OleDb;
using System.Security.AccessControl;
using ADOX;
namespace TestDataLog
{
public class TestDataLog
{
OleDbConnection dbConnection = null;
OleDbConnectionStringBuilder oleDbConnectionStringBuilder = null;
OleDbCommand command;
public string FileName { get; set; }
public string TableName { get; set; }
public TestDataLog(string tableName, string path, string fileName = "TestDataLog.Mdb")
{
try
{
TableName = tableName;
FileName = fileName;
dbConnection = new OleDbConnection();
oleDbConnectionStringBuilder =
new OleDbConnectionStringBuilder();
oleDbConnectionStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
path = Path.Combine(path, FileName);
oleDbConnectionStringBuilder.DataSource = path;
oleDbConnectionStringBuilder.ConnectionString += ";Jet OLEDB:Engine Type=5";
if (!File.Exists(path))
{
Catalog catalog = new Catalog();
catalog.Create(oleDbConnectionStringBuilder.ConnectionString);
}
this.Open();
this.CreateTable();
}
catch (Exception ex)
{
this.Close();
throw ex;
}
}
public void Log(string serial, bool testResult)
{
try
{
command = dbConnection.CreateCommand();
command.CommandText = "INSERT INTO " + TableName +
" (DateTimeLog," +
" SN," +
" TestResult)" +
" VALUES(#" +
DateTime.Now + "#, '" +
serial + "', " +
testResult + ")";
command.ExecuteNonQuery();
this.Close();
}
catch (Exception ex)
{
this.Close();
throw ex;
}
}
public void Open()
{
if (!(dbConnection.State == System.Data.ConnectionState.Open))
{
dbConnection.ConnectionString =
oleDbConnectionStringBuilder.ConnectionString;
dbConnection.Open();
}
}
public void Close()
{
if (dbConnection != null)
if (!(dbConnection.State == System.Data.ConnectionState.Closed))
{
dbConnection.Close();
dbConnection = null;
}
}
public void CreateTable()
{
try
{
command = dbConnection.CreateCommand();
command.CommandText =
"CREATE TABLE " + TableName + " (" +
"[Count] IDENTITY NOT NULL PRIMARY KEY, " +
"[DateTimeLog] TIMESTAMP NOT NULL, " +
"[SN] VARCHAR(50) NOT NULL, " +
"[TestResult] BIT NOT NULL)";
command.ExecuteNonQuery();
}
catch (OleDbException ex)
{
if (!(ex.Message.StartsWith("Table") && ex.Message.EndsWith("already exists.")))
{
this.Close();
throw ex;
}
}
}
}
}
Add Mode=Share Deny None to your connection string. This will open your database file in full share mode. Now it's up to you to manage concurrency, though. :-)
Of Course Yes.
..Just make sure that your database is placed in a shared folder that other workstation can also view via Network. Then use the file path where your DB is located like:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\MySharedFolder\\TestDataLog.Mdb
MS ACESS Connection
Regards..