How to import data from a .DBF file into SQL Server? - sql-server

So far I have a successful connection retrieving data from a .dbf file but I can not figure out how to store this data into my database. Currently I have my connection with SQL Server and connection string name is PortageleafPickupEntities1 which holds three columns Address, SeasonType, and NextPickupdate.
Now this .dbf data has all the data for each column just got to tranfer it there.
Here is the code...
public HttpResponseMessage Post([FromBody]LeafPickup pickup )
{
string cadena = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\Users\\azib\\Desktop\\dfb; Extended Properties = dBASE IV; User ID = Admin; Password =";
string select = "Select * from sp18br ";
DataSet myDataSet = new DataSet();
OleDbConnection con = new OleDbConnection(cadena);
OleDbCommand myAccessCommand = new OleDbCommand(select, con);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
con.Open();
myDataAdapter.Fill(myDataSet, "sp18br");
con.Close();
DataTableCollection dta = myDataSet.Tables;
foreach (DataTable dt in dta)
{
Console.WriteLine("Found data table {0}", dt.TableName);
}
Console.WriteLine("{0} rows in sp18br table", myDataSet.Tables["sp18br"].Rows.Count);
DataRowCollection dra = myDataSet.Tables["sp18br"].Rows;
foreach (DataRow dr in dra)
{
// Print the CategoryID as a subscript, then the Name:
Console.WriteLine("\n" + " {0} -- {1} -- {2} -- {3} -- {4} ", "\n" + dr[0], dr[1], dr[2], dr[3], dr[4]);
}
using (PortageLeafPickupEntities1 entities = new PortageLeafPickupEntities1())
{
entities.LeafPickups.Add(pickup);
entities.SaveChanges();
var message = Request.CreateResponse(HttpStatusCode.Created, pickup);
message.Headers.Location = new Uri(Request.RequestUri + pickup.ID.ToString());
return message;
}
}

Related

Store and retrieve data table into Varbinary column

For some reason I had to store a DataTable variable in a Varbinary column of a SQL Server table, but I get errors.
This is store code:
// Read DataTable to Byte array
DataTable dtgrd = new DataTable();
DataAccess ds = new DataAccess();
DataSet dst = new DataSet();
dst.Tables.Add(dtgrd);
string xmlString = dst.GetXml();
MemoryStream ms = new MemoryStream();
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlString);
xml.Save(ms);
byte[] xmlBytes = ms.ToArray();
// Store DataTable into database
SqlConnection CN = new SqlConnection(Global.cs);
string str = #"INSERT INTO...... ";
SqlCommand SqlCom = new SqlCommand(str, CN);
SqlCom.Parameters.Add(new SqlParameter("#FKDocInReqID", FKDocInReqID));
SqlCom.Parameters.Add(new SqlParameter("#value", (object)xmlBytes));
SqlCom.Parameters.Add(new SqlParameter("#Data", ReportTitle));
SqlCom.Parameters.Add(new SqlParameter("#DocUploadUser_secuserID", Global.S_UserID));
CN.Open();
decimal id =(decimal) SqlCom.ExecuteScalar();
CN.Close();
The store code has worked correctly and in the database, the columns has the proper bytes.
But the retrieve code does not work:
string s = #"select * from .... where id={0} ";
s = string.Format(s, id);
DataTable dt = new DataTable();
dt = ds.doSelect(s);
using (System.IO.MemoryStream memStream1 = new System.IO.MemoryStream((byte[])dt.Rows[0]["Value"]))
{
dataGridView1.DataSource = FromBytes(memStream1.ToArray());
dataGridView1.Refresh();
dataGridView1.Show();
}
and
static DataTable FromBytes(byte[] arr)
{
using (var ms = new MemoryStream(arr))
{
return (DataTable)new BinaryFormatter().Deserialize(ms);//**ERROR Raised Here**
}
}
static byte[] ToBytes(DataTable table)
{
using (var ms = new MemoryStream())
{
table.RemotingFormat = SerializationFormat.Binary;
new BinaryFormatter().Serialize(ms, table);
return ms.ToArray();
}
}
this error is raised:
error message
The example code on Microsoft's website uses XmlTextReader
static DataTable FromBytes(Stream st)
{
var ds = new DataSet();
using (XmlTextReader xmlReader = new XmlTextReader(st))
{
ds.ReadXml(xmlReader);
}
return ds.Tables[0];
}
I'll leave you to think about the obvious design flaws in storing one table within another.
this is how I figure out this problem maybe help to someone else.
instead of save data into Varbinary field save it to xml in nvarchar(max) and then retrieve it :
//Retrieve Process:
string s = #"select * from .... where id={0} ";
s = string.Format(s, ID);
DataTable dtlist = new DataTable();
DataAccess ds = new DataAccess();
dtlist = ds.doSelect(s);//doselect is function that run select sql command
XmlDocument xml = new XmlDocument();
xml.LoadXml(dtlist.Rows[0]["Data"].ToString());//data is column name that store xml in it
StringReader theReader = new StringReader(xml.InnerXml);
DataSet theDataSet = new DataSet();
theDataSet.ReadXml(theReader);
gridEX1.DataSource = theDataSet.Tables[0];

How to import data from Excel sheet to database in c# with LINQ?

I have three columns in Excel sheet such as id, name, family.
I am using LINQ and i need to import data from Excel to database with coding instruction, i have 6500 records in Excel sheet
You can use below code to get all the data and then you can convert form DataTable to List. for below example to work you have Microsoft Access Database Engine 2010 Redistributable
should be installed
public static DataTable ReadExcelWithoutOffice(string filePath)
{
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=YES;TypeGuessRows=0;FirstRowHasNames=true;ImportMixedTypes=Text\""; ;
using (var conn = new OleDbConnection(connectionString))
{
conn.Open();
var sheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT * FROM [" + sheets.Rows[0]["TABLE_NAME"].ToString() + "] ";
var adapter = new OleDbDataAdapter(cmd);
var ds = new DataSet();
adapter.Fill(ds);
return ds.Tables[0];
}
}
}
Thank you all for your answer.
I found my problem. Here is the code :
string pach = #"D:\C# Projects\ex.xlsx";
var excelData = new ExcelQueryFactory(pach);
var data = from x in excelData.Worksheet<xlsdata>("MySheet")
select x;
DataClassesDataContext db = new DataClassesDataContext();
foreach (var d in data)
{
db.tbl_infos.InsertOnSubmit(new tbl_info
{
id = d.id,
name = d.name,
family = d.family
});
}
db.SubmitChanges();
You will need to import and reference OpenXML, open the sheets, sheet, worksheet, IIRC - then parse through your columns into strings.
OpenXML
Then create a SQL Data Adapter and all of that, to use either a ConnectionString or SQLConnection, fire up a parameterized query, and it's in the database.
SQL Example

Stored Procedure returns a temporary table that I need to convert to a CSV file

I basically have a stored procedure that I call through a method:
Time_Tracker.BLL.ResultsManager.GetCSV(Convert.ToDateTime("2014-01-11"));
It returns 8 columns of data ranging from 25 to 150 records.
I need to be able to convert it to a CSV file to a path of the users choosing. I am able to get it into my code behind as an array ( Results[] TEST = new Results[25]; ) and have verified that the data is O.K. I see plenty of posts were they use a DataTable as a source to convert to CSV, but I am not sure how to load a DataTable from the method that calls the stored procedure. Same thing with DataGridView, not sure how to load the data into a DataGridView either.
I have also seen methods were they use SqlDataAdapter to populate a DataTable. Since I use methods to that work directly with stored procedures, I don't want to have to use SqlDataAdapter and provide the database configuration info each time.
IF someone could help me load it into a DataTable or DataGridView, I think I can figure it out from there.
Thank you in advance.
Eric
Just define a data table and use the load command to move the data from the reader to the data table.
http://msdn.microsoft.com/en-us/library/d7125bke.aspx
This is right from MSDN. I added two lines to load a data table.
-- Code from msdn
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
SqlDataReader reader = cmd.ExecuteReader();
-- This is my addition
DataTable dt = new DataTable();
dt.Load(reader);
sqlConnection1.Close();
I still do not understand what you are trying to do. You can create a data table, xml record set, via coding.
The code below can be used to translate an array to a DataTable. Some work is needed on your side to add your details.
http://msdn.microsoft.com/en-us/library/skef0k7a(v=vs.110).aspx
// Create sample Customers table.
DataTable table = new DataTable();
table.TableName = "Customers";
// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };
table.Rows.Add(new object[] { 0, "Mary" });
table.Rows.Add(new object[] { 1, "Andy" });
table.Rows.Add(new object[] { 2, "Peter" });
table.AcceptChanges();
return table;
I hope this helps. If not, I still do not get the business requirement.
I figured it out (See Below). I will mark this as solved and once again, Thank You for your help !
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
if (dr[i] is DateTime)
{
if (((DateTime)dr[i]).TimeOfDay.TotalSeconds == 0) // Time Since Midnight (in Seconds)
{
sw.Write(((DateTime)dr[i]).ToString("yyyy-MM-dd"));
}
}
else
{
sw.Write(dr[i].ToString());
}
}
if (i < iColCount - 1) sw.Write(",");
}
sw.Write(sw.NewLine);
}
Here is what I ended up doing for anyone else looking for help:
protected void btnCSV_Click(object sender, EventArgs e)
{
try
{
// HATE EXPOSING THE DATABASE CONNECTION THIS WAY !!!
SqlConnection sqlConnection1 = new SqlConnection(DAL.DBUtils.SqlConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "[dbo].[usp.CSV_OUT]";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#DateBeg", Time_Tracker.Utilities.TimeCard_Start_Date());
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
sqlConnection1.Close();
ExportToCSV(dt, ConfigurationManager.AppSettings["CSVPath"].ToString(), "CSV_Hours_Data_" + Time_Tracker.Utilities.TimeCard_Start_Date().AddDays(+6).ToString("MM_dd_yyyy") + ".csv");
}
catch (Exception ex)
{
Utilities.ErrorLog(ex.Message, ex.GetType().ToString(), ex.StackTrace, #"Time_Tracker.txt");
}
}
public static void ExportToCSV(DataTable dt, string strFilePath, string fileName)
{
try
{
var sw = new StreamWriter(strFilePath + fileName, false);
// Write the headers.
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1) sw.Write(",");
}
sw.Write(sw.NewLine);
// Write rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
string output = dr[i].ToString();
if (dr[i] is DateTime)
{
if (((DateTime)dr[i]).TimeOfDay.TotalSeconds == 0) // Time Since Midnight (in Seconds)
{
output = (((DateTime)dr[i]).ToString("yyyy-MM-dd"));
}
}
if (output.Contains(";") || output.Contains("\""))
output = '"' + output.Replace("\"", "\"\"") + '"';
if (Regex.IsMatch(output, #"(?:\r\n|\n|\r)"))
output = string.Join(" ", Regex.Split(output, #"(?:\r\n|\n|\r)"));
sw.Write(output);
}
if (i < iColCount - 1) sw.Write(",");
}
sw.Write(sw.NewLine);
}
sw.Close();
// Causes Save As Dialog box to appear for user.
String FileName = fileName;
String FilePath = strFilePath;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();
}
catch (Exception ex)
{
Utilities.ErrorLog(ex.Message, ex.GetType().ToString(), ex.StackTrace, #"Time_Tracker.txt");
}
}

Uploading excel in to SQL Server in ASP.NET

When I try to upload an Excel sheet of data into SQL Server I get the following error first time it uploads well when I try to upload it shows the following error
Before uploading I am deleting files in the directory
string[] filePaths1 = Directory.GetFiles(Server.MapPath(#"Excel\"));
foreach (string filePath1 in filePaths)
File.Delete(filePath1);
The process cannot access the file
'C:\inetpub\vhosts\xyz.com\httpdocs\newofficework\Excel\super.xlsx'
because it is being used by another process.
You seem to be performing multiple actions here (1) Delete and then (2) Upload
How are you Uploading the files? If you are using the SaveAs method, then please ensure that you Dispose the same.
E.g. (fuExcel is my FileUpload control)
fuExcel.SaveAs(sUniqueFilePath); // string variable containing file path
fuExcel.Dispose();
The Page having a FileUpload control and the Upload button, on selecting the Excel file user needs to click on Upload button to store the data to Server. Here we are treating the uploaded file as database hence we need to create OLEDB connection to this file, from this connection will be created and the data is fetched to C# as DataTable. '[Sheet1$]' is the Name of the Worksheet where requires data is present.
string SourceConstr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select * from [Sheet1$]";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(dtExcel);
From this code connection will be created for the Excel file and the data in Sheet1 will be dumped to the dtExcel of type DataTable.
Note:'$' must be there after the worksheet name.
fileName = FileUpload1.ResolveClientUrl(FileUpload1.PostedFile.FileName);
int count = 0;
DataClassesDataContext conLinq = new DataClassesDataContext("Data Source=server name;Initial Catalog=Database Name;Integrated Security=true");
try
{
DataTable dtExcel = new DataTable();
string SourceConstr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fileName + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select * from [Sheet1$]";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(dtExcel);
for (int i = 0; i < dtExcel.Rows.Count; i++)
{
try
{
count += conLinq.ExecuteCommand("insert into table name values(" + dtExcel.Rows[i][0] + "," + dtExcel.Rows[i][1] + ",'" + dtExcel.Rows[i][2] + "',"+dtExcel.Rows[i][3]+")");
}
catch (Exception ex)
{
continue;
}
}
if (count == dtExcel.Rows.Count)
{
<--Success Message-->
}
else
{
<--Failure Message-->
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
conLinq.Dispose();
}
Add these two name space in your class file
using System.Data.OleDb;
using System.Data.SqlClient;
Use following code
public void importDataFromExcel(string excelFilePath)
{
//Declare Variables - Edit these based on your particular situation
string sSQLTable = "tDataMigrationTable";
// make sure your sheet name is correct, here sheet name is Sheet1, so you can change your sheet name if have different
string myExcelDataQuery = "Select StudentName,RollNo,Course from [Sheet1$]";
try
{
//Create our connection strings
string sExcelConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";
string sSqlConnectionString = "SERVER=MyDatabaseServerName;USER ID=DBUserId;PASSWORD=DBUserPassword;DATABASE=DatabaseName;CONNECTION RESET=FALSE";
//Execute a query to erase any previous data from our destination table
string sClearSQL = "DELETE FROM " + sSQLTable;
SqlConnection SqlConn = new SqlConnection(sSqlConnectionString);
SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
SqlConn.Open();
SqlCmd.ExecuteNonQuery();
SqlConn.Close();
//Series of commands to bulk copy data from the excel file into our SQL table
OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
OleDbConn.Open();
OleDbDataReader dr = OleDbCmd.ExecuteReader();
SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString);
bulkCopy.DestinationTableName = sSQLTable;
while (dr.Read())
{
bulkCopy.WriteToServer(dr);
}
OleDbConn.Close();
}
catch (Exception ex)
{
//handle exception
}
}

Writing an SQL OleDbCommand to import data from SqlServer To An Access Database

I have the following code :
ADOX.Catalog cat = new ADOX.Catalog();
string pathToNewAccessDatabase = "Data Source=D:\\Data\\NewMDB.mdb";
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + pathToNewAccessDatabase + ";Jet OLEDB:Engine Type=5");
System.Data.OleDb.OleDbConnection AccessConn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + pathToNewAccessDatabase);
AccessConn.Open();
System.Data.OleDb.OleDbCommand AccessCommand = new System.Data.OleDb.OleDbCommand("SELECT * INTO [ReportFile] FROM [Data Source=server_path; Initial Catalog=database_name; User Id=user_name; Password=pass_word;Trusted_Connection=False].[dbo.DataSourceTable]", AccessConn);
AccessCommand.ExecuteNonQuery();
AccessConn.Close();
I want to select from SQL SERVER into the ACCESS database.
Also, if the password contains the [ character, how do I escape that ?
I suggest establishing an SQLConnection first to the SQL server and query your desired data into a DataTable.
using (SqlConnection conn = new SqlConnection("yourConnectionString"))
{
using (SqlCommand comm = new SqlCommand("Select columns from targetTable", conn))
{
SqlDataReader reader = comm.ExecuteReader();
DataTable tbl = new DataTable();
tbl.Load(reader);
}
}
After you have the data in your datatatable, create the query for your insert command from it by looping through the data.
string insertCommandString = string.Empty;
for (int row = 0; row < tbl.Rows.Count; row++)
{
insertCommandString = "Insert into yourTableName(yourColumnNames) values(";
for (int column = 0; column < tbl.Columns.Count; column++)
{
if(tbl.Columns[column].DataType == typeof(String))
{
insertCommandString += "'" + tbl.Rows[row][column].ToString() + "'";
}
else
{
insertCommandString += tbl.Rows[row][column].ToString();
}
if (column < tbl.Columns.Count - 1)
{
insertCommandString += ",";
}
}
insertCommandString += ")";
System.Data.OleDb.OleDbCommand AccessCommand = new System.Data.OleDb.OleDbCommand(insertCommandString, AccessConn);
AccessCommand.ExecuteNonQuery();
}
You should not escape any character from password.

Resources