I have uploaded and stored multiple txt files into a SQL Server database. The idea here is that a user can select the files that they want and then have the C# code read the data from each file and create ONE text file with all the data from each selected. Then make the consolidated file available for download
Any help would be much appreciated.
StreamWriter Writer = new StreamWriter("Filename");
string SQL = "select Item from Table where ID in (1, 2, 3)";
SqlCommand Command = new SqlCommand(SQL, Con);
SqlDataReader Reader = Command.ExecuteReader();
while (Reader.Read())
Writer.WriteLine(Reader["Item"].ToString());
Reader.Close();
Writer.Close();
Making the file available for download is another question.
Related
From a third party I am receiving a SQLite '.db3' database file. I wish to read the data stored within the database in an Azure function, but have no requirement to modify the database.
In my Azure function I have also uploaded a text file alongside the .db3 file, I can read the contents of this text file just fine, meaning that I have the URI and read privileges on the folder.
When I try to read some data from the database I get the error 'database is locked'.
Code to read the database is below. This works locally.
List<object> obs = new List<object>();
using(var con = new SQLiteConnection($"URI=file:{fullUri};mode=ReadOnly"))
{
con.Open();
using(var cmd = new SQLiteCommand(sql, con))
{
using(SQLiteDataReader rdr = cmd.ExecuteReader())
{
while(rdr.Read())
{
obs.Add(rdr.GetValues());
}
}
}
con.Close();
}
return obs;
How do I read the database?
I've had this problem before, reading a local file ... more than once. These are the steps I took depending on the scenario:
If your Azure Function is deployed using zipdeploy then under the "Configuration" > "Application Settings", change "WEBSITE_RUN_FROM_PACKAGE" from 1 to 0.
If the file you are trying to reference is saved under wwwroot on the Azure Function I would try put it somewhere else e.g. d:\local.
Hello I am working in windows application "winforms" in this I have successfully save pdf file in database now I want to display that in adobe reader which i have been added from toolbox
what should I need to do, thats why I can able to view pdf in adobe reader.
I already try to display pdf file to reader but nothing display.
Here is my code:
{
con.Open();
SqlCommand command1 = new SqlCommand("select PDFFILE from help ;", con);
DataTable dt = new DataTable();
byte[] img = (byte[])command1.ExecuteScalar();
MemoryStream ms = new MemoryStream(img);
string s = System.Text.Encoding.UTF8.GetString(img);
axAcroPDF1.LoadFile(s);
con.Close();
}
If the adobe control only takes a filename, you will need to save the pdf as a file first. System.IO.Path will get you a temporary path. Using a guid will also prove useful for random names.
If you don't want that, you may use the PdfiumViewer, which is Googles Pdfium wrapped as a .NET control. Works fine, takes streams as load parameter and is easy to use. You can get both the PdfiumViewer and the required Pdfium-DLLs via NuGet. Plus, you won't be dependent on the installed adobe version any more.
I need a function to transfer data from a .DBF file to SQL Server.
Here is what I do:
First step: I use OleDBDataAdapter.Fill to read from .DBF file
Second step: insert this table into SQL Server.
The first step takes 74 seconds for 90 column X 80000 rows.
Is there any way to speed this process up?
Also, if there is any way to communicated directly from .DBF to SQL Server, please guide me. BTW, I am using C# and SQL Server 2008.
My mistake, guys. I rarely post here.
Here is my code (take over 1 minute to transfer DBF into datatable):
OleDbCommand oledbcommand = new OleDbCommand();
OleDbDataAdapter adp = new OleDbDataAdapter();
oledbcommand.Connection = oledbConnectOpen();
oledbcommand.CommandText = #"SELECT * FROM " + filename;
adp.SelectCommand = oledbcommand;
adp.Fill(dt); //this is the step that consume time
dt.TableName = filename;
return dt;
In this scenario you would be better using an OleDbDataReader as opposed to OleDBDataAdapter because the reader is optimised for forward-only, read-only access. See this article.
I need to connect to an open Excel 2003 file using .NET 3.5
It seems the OleDb connection which I am trying to use wants the file exclusively. But I need to have this file open in Excel in the same time.
Is non-locking reading possible?
EDIT: I resolved this by copying file before opening it.
the question seems have no answer. and I cant delete it....
my solution was - run macro on timer to save the excel file in question and C# app was copying the file to another one and reading it using OleDb.
This seems like a similar problem:
Writing into excel file with OLEDB
Does that work out for you?
What parameters are you passing in when you open the Excel document? Could you set the "ReadOnly" parameter in Workbook.Open() to true? See here.
Refer to the code below how to get the information of Excel data into an array. Then you will perform any validations on that Excel sheet.
var fileName = #"D:\Pavan\WorkDiployed.xlsx";
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=Excel 12.0;", fileName);
OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", con);
con.Open();
System.Data.DataSet excelDataSet = new DataSet();
cmd.Fill(excelDataSet);
DataTable data = excelDataSet.Tables[0];
DataRow[] arrdata = data.Select();
foreach (DataRow rw in arrdata)
{
object[] cval = rw.ItemArray;
}
con.Close();
MessageBox.Show (excelDataSet.Tables[0].ToString ());
I have a DBF file and a index file.
I want to read index file and search records satisfy some condition.
(for example: search records which its StudentName begin with "A" by using Student.DBF and StudentName.idx)
How do I do this programmatically?
It would be easiest to query via OleDB Connection
using System.Data.OleDb;
using System.Data;
OleDbConnection oConn = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=C:\\PathToYourDataDirectory");
OleDbCommand oCmd = new OleDbCommand();
oCmd.Connection = oConn;
oCmd.Connection.Open();
oCmd.CommandText = "select * from SomeTable where LEFT(StudentName,1) = 'A'";
// Create an OleDBAdapter to pull data down
// based on the pre-built SQL command and parameters
OleDbDataAdapter oDA = new OleDbDataAdapter(oCmd);
DataTable YourResults
oDA.Fill(YourResults);
oConn.Close();
// then you can scan through the records to get whatever
String EachField = "";
foreach( DataRow oRec in YourResults.Rows )
{
EachField = oRec["StudentName"];
// but now, you have ALL fields in the table record available for you
}
I dont have the code off the top of my head, but if you do not want to use ODBC, then you should look into reading ESRI shape files, they consist of 3 parts (or more) a .DBF (what you are looking for), a PRJ file and a .SHP file. It could take some work, but you should be able to dig out the code. You should take a look at Sharpmap on codeplex. It's not a simple task to read a dbf w/o ODBC but it can be done, and there is a lot of code out there for doing this. You have to deal with big-endian vs little-endian values, and a range of file versions as well.
if you go here you will find code to read a dbf file. specifically, you would be interested in the public void ReadAttributes( Stream stream ) method.