Importing DBF file in SQL Server 2012 - import

I have over 200+ dbf files having different schema. I have used SSIS to import .dbf files, but in order to automate the task I want to use OpenRowset. OpenRowSet is working fine for Excel Files, but not for .dbf.
I have written
SELECT [LRSNum],[AppUpdD],[AppStat],[PIN] FROM OPENROWSET('MICROSOFT.ACE.OLEDB.12.0','dBASE 5.0;Database=D:\Sales- data\SalesSourcefile\2016\December\Shape\Martin\real_land\', 'SELECT [LRSNum],[AppUpdD],[AppStat],[PIN] FROM real_land.dbf');
Any help will be appreciated.
I am using SQL Server 2012, Windows 8.1.
Installed Foxpro driver, but when selected foxpro using DTS, it fails.

FYI - You can do automated uploads of DBFs to SQL Server using SQL Server Upsizing Wizard for FoxPro.
With this tool, you have to do the uploading from FoxPro, and the DBFs must be attached to a FoxPro DBC.
http://www.codemag.com/article/0703052
The latest version if available from the VFPx site.

Finally, this is working
SELECT * FROM OPENROWSET ('MICROSOFT.ACE.OLEDB.12.0','dBase 5.0;HDR=YES;IMEX=2;DATABASE=\Dbf Directory\',
'SELECT * FROM dbf_filename.dbf')

I once had to do this too and wrote some C# code for the import.
Just as a try: With this line I open the connection within C#
var con = new OdbcConnection("Driver={{Microsoft dBASE Driver (*.dbf)}};Dbq=C:\\SomePath;DriverID=277;");
Might be, that you can get something out of this.
Some C# code
The following code is taken from one of my C# projects. I modified it to be neutral, but I cannot guarantee, that this is working right so:
public List<string> Ambus;
public List<string> Tbls;
public List<string> errList;
public List<string> SQLs;
private void btnImport_Click(object sender, EventArgs e) {
SqlConnection sqlcon;
SqlCommand sqlcmd;
SQLs.Clear();
Tbls.Clear();
var con = new OdbcConnection("Driver={{Microsoft dBASE Driver (*.dbf)}};Dbq=C:\\SomePath;DriverID=277;");
con.Open();
var tbls = con.GetSchema(OdbcMetaDataCollectionNames.Tables);
foreach (System.Data.DataRow r in tbls.Rows) {
Tbls.Add(r["TABLE_NAME"].ToString());
}
DataTable cols = null;
var sb = new StringBuilder();
int i = 0;
foreach (var tblnm in Tbls) {
i++;
sb.Clear();
try {
cols = con.GetSchema(OdbcMetaDataCollectionNames.Columns, new string[] { null, null, tblnm, null });
sb.AppendFormat(" CREATE TABLE dbo.[{0}](TableName VARCHAR(100) NOT NULL ", tblnm);
int count = 0;
foreach (DataRow colrow in cols.Rows) {
var colInf = string.Format(" ,{0} {1} NULL", colrow["COLUMN_NAME"].ToString(), this.createDataType(colrow["TYPE_NAME"].ToString(), colrow["COLUMN_SIZE"].ToString(), colrow["DECIMAL_DIGITS"].ToString(), colrow["NUM_PREC_RADIX"].ToString()));
sb.Append(colInf);
count++;
}
sb.Append("); ");
SQLs.Add(sb.ToString());
sb.Clear();
var cmd = new OdbcCommand("SELECT * FROM [" + tblnm + "]", con);
var reader = cmd.ExecuteReader();
while (reader.Read()) {
var vals = createVals(cols, reader, tblnm);
string insStat = string.Format(" INSERT INTO dbo.[{0}] VALUES ('{0}',{1});", tblnm, vals);
SQLs.Add(insStat);
}
}
catch (Exception exo) {
errList.Add(string.Format("{0}:{1}", tblnm, exo.Message));
}
con.Close();
sqlcon = new SqlConnection("Data Source=SomeSQLServer;Initial Catalog=master;User ID=sa;pwd=SomePwd");
sqlcon.Open();
sqlcmd = new SqlCommand("USE SomeTargetDB;", sqlcon);
sqlcmd.ExecuteNonQuery();
i = 0;
foreach (string s in SQLs) {
i++;
//Progress-output: this.Text = string.Format("{0} von {1}", i, SQLs.Count);
sqlcmd = new SqlCommand(s, sqlcon);
sqlcmd.ExecuteNonQuery();
}
sqlcon.Close();
}
}
private string createDataType(string typ, string size, string dec, string prec) {
switch (typ.ToLower()) {
case "char":
return "NVARCHAR(" + size + ")";
case "logical":
return "BIT";
case "numeric":
dec = dec == string.Empty ? null : dec;
prec = prec == string.Empty ? null : prec;
int d = int.Parse(dec ?? "0");
int p = int.Parse(prec ?? "0");
if (d == 0 && p == 0)
return "INT";
else if (d > 0 && p > 0)
return string.Format("DECIMAL({0},{1})", d, p);
else if (d == 0 && p > 0)
return "FLOAT";
else
return null;
case "date":
return "DATETIME";
default:
return null;
}
}
private string createVals(DataTable cols, OdbcDataReader reader, string tblnm) {
var sb = new StringBuilder();
sb.Append("'" + tblnm + "'");
foreach (DataRow colrow in cols.Rows) {
var val = string.Empty;
try {
val = reader[colrow["COLUMN_NAME"].ToString()].ToString();
}
catch { }
if (val.Trim().Length == 0)
val = "NULL";
else {
if (colrow["TYPE_NAME"].ToString().ToLower() == "char")
val = val.Replace("'", "''");
if (colrow["TYPE_NAME"].ToString().ToLower() == "numeric")
val = val.Replace(".", "").Replace(",", ".");
if (colrow["TYPE_NAME"].ToString().ToLower() == "date") {
var d = DateTime.Parse(val, System.Globalization.CultureInfo.CurrentCulture);
if (d.Year < 1900 || d.Year > 2020)
d = new DateTime(1900, d.Month, d.Day, d.Hour, d.Minute, d.Second);
val = d.ToString("dd.MM.yyyy HH:mm:ss");
}
val = "'" + val + "'";
}
sb.AppendFormat(",{0}", val);
}
return sb.ToString();
}

Related

Postgres function is not working/calling in hosted web project

I have created an ASP.Net Core Web App (MVC) Project which in turns call Web Api for calling the database for getting the values and posting the values. The database used is Postgres. I have written a Postgres function to populate values for a report Day Book. When I run the project through visual studio it works fine. Then I have hosted the project. When I login through hosted ip address and calling the report function returns no values and the reports showing no values.
The Postgres function is given below
CREATE OR REPLACE FUNCTION public.day_book(
d1 date,
d2 date,
cid integer)
RETURNS TABLE(roworder integer, guid uuid, tbl1 text, date1 timestamp without time zone, credit real, debit real, description1 text, credit1 real, debit1 real)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
BEGIN
RETURN QUERY
SELECT db1."roworder",db1."guid",db1."tbl1",db1."date1",db1."credit",db1."debit",
db1."description1",db1."credit1",db1."debit1"
from "tbldaybook1" as db1 order by db1."date1",db1."roworder";
END;
$BODY$;
The MVC controller method that calls the API controller method is given below
public async Task<JsonResult> GetDayBook(DateTime FromDate, DateTime ToDate)
{
ClaimsPrincipal currentUser = this.User;
var currentUserId = Convert.ToInt32(currentUser.FindFirst("UserId").Value);
var ChurchId = Convert.ToInt32(currentUser.FindFirst("ChurchId").Value);
var Role = currentUser.FindFirst(ClaimTypes.Role).Value;
var isSuperAdmin = Convert.ToBoolean(currentUser.FindFirst("IsSuperAdmin").Value);
var d11 = FromDate.ToString("dd-MM-yyyy");
var d22 = ToDate.ToString("dd-MM-yyyy");
int cid = ChurchId;
List<RptDayBook> daybook = new List<RptDayBook>();
HttpClient client = api.Initial();
HttpResponseMessage responseTask = await client.GetAsync("reports/GetDayBook/" + d11 + "/" + d22 + "/" + cid);
if (responseTask.IsSuccessStatusCode)
{
var readTask = responseTask.Content.ReadAsStringAsync().Result;
daybook = JsonConvert.DeserializeObject<List<RptDayBook>>(readTask);
}
return Json(daybook);
}
The below API method connects to the Postgres database and call the function
[HttpGet("GetDayBook/{d11}/{d22}/{cid}")]
public IEnumerable<RptDayBook> GetDayBook(string d11, string d22, int cid)
{
using (var command = db.Database.GetDbConnection().CreateCommand())
{
DateTime d1 = Convert.ToDateTime(d11);
DateTime d2 = Convert.ToDateTime(d22).AddDays(1);
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "day_book";
command.Parameters.Add(new Npgsql.NpgsqlParameter("d1", NpgsqlTypes.NpgsqlDbType.Date)
{ Value = d1 });
command.Parameters.Add(new Npgsql.NpgsqlParameter("d2", NpgsqlTypes.NpgsqlDbType.Date)
{ Value = d2 });
command.Parameters.Add(new Npgsql.NpgsqlParameter("cid", NpgsqlTypes.NpgsqlDbType.Integer)
{ Value = cid });
if (command.Connection.State == ConnectionState.Closed)
command.Connection.Open();
List<object[]> result = new List<object[]>();
var res = command.ExecuteReader();
if (res.HasRows)
{
while (res.Read())
{
var values = new object[res.FieldCount];
for (int i = 0; i < res.FieldCount; i++)
{
if (i == 4 || i == 5 || i == 7 || i == 8)
{
values[i] = res[i] == DBNull.Value ? 0 : Convert.ToDouble(res[i]);
}
else
{
values[i] = res[i];
}
}
result.Add(values);
}
}
List<RptDayBook> rpt = new List<RptDayBook>();
foreach (var item in result)
{
RptDayBook rptDayBook = new RptDayBook();
rptDayBook.roworder = (int)item[0];
rptDayBook.GUID = (Guid)item[1];
rptDayBook.tbl1 = (string)item[2];
rptDayBook.date1 = (DateTime)item[3];
rptDayBook.credit = item[4] == DBNull.Value ? 0 : Convert.ToDouble(item[4]);
rptDayBook.debit = item[5] == DBNull.Value ? 0 : Convert.ToDouble(item[5]);
rptDayBook.Description1 = (string)item[6];
rptDayBook.credit1 = item[7] == DBNull.Value ? 0 : Convert.ToDouble(item[7]);
rptDayBook.debit1 = item[8] == DBNull.Value ? 0 : Convert.ToDouble(item[8]);
rpt.Add(rptDayBook);
}
return rpt;
}
}
What will be the issue? Is it either the function is not calling or function can't return values?
It is the issue while converting the datetime in the API.
Change the code from
DateTime d1 = Convert.ToDateTime(d11);
to
DateTime d1 = DateTime.ParseExact(d11, "dd-MM-yyyy", CultureInfo.InvariantCulture);
Also do the same in the next line also.

Mongodb processes only one record at a time?

I am using Mongodb in .net core.
At the same time I have many applications that query the same table. I have the problem that 1 record is processed in many applications.
I used the version in mongo but it didn't work well.
Here is my code:
try
{
TopupRequest topupReturn = null;
do
{
var topupRequest = await _paygateMongoRepository.GetOneAsync<TopupRequest>(p =>
p.Status == TopupStatus.Init && categoryCodes.Contains(p.CategoryCode) &&
p.ServiceCode == serviceCode);
if (topupRequest == null)
break;
var version = topupRequest.Version;
var versionPlus = topupRequest.Version + 1;
Expression<Func<TopupRequest, bool>> filter = p => p.Id == topupRequest.Id && p.Version == version;
var options = new FindOneAndUpdateOptions<TopupRequest, TopupRequest>
{
IsUpsert = false,
ReturnDocument = ReturnDocument.After
};
_logger.Info("Version: " + version + " version plus: " + versionPlus);
topupReturn = await _paygateMongoRepository.GetAndUpdateOne<TopupRequest, Guid>(filter,
Builders<TopupRequest>.Update.Set(m => m.Status, TopupStatus.InProcessing)
.Set(m => m.Version, versionPlus)
.Set(m => m.WorkerApp, workerApp), options);
_logger.Info("Version: " + version + " version plus: " + versionPlus);
} while (topupReturn == null);
if (topupReturn == null) return null;
if (timeout > 0 && topupReturn.CreatedTime.AddSeconds(timeout) <= DateTime.UtcNow) return null;
_logger.Info($"GetTopupPriorityAvailableVersionAsync success: {topupReturn.ToJson()}");
return topupReturn.ConvertTo<TopupRequestDto>();
}
catch (Exception e)
{
_logger.Info($"GetTopupPriorityAvailableVersionAsync error: {e}");
return null;
}
I used a transaction solution. But it also doesn't work for me.
using (var session = MongoDbContext.Client.StartSession())
{
var topups = MongoDbContext.GetCollection<TopupRequest>();
try
{
session.StartTransaction(new TransactionOptions(
readConcern: ReadConcern.Local,
readPreference: ReadPreference.Primary,
writeConcern: WriteConcern.WMajority));
var sort = Builders<TopupRequest>.Sort.Descending("CreatedTime");
var filter = Builders<TopupRequest>.Filter.Eq(p => p.Status, TopupStatus.Init)
& Builders<TopupRequest>.Filter.In("CategoryCode", categoryCodes)
& Builders<TopupRequest>.Filter.Eq(p => p.ServiceCode, serviceCode);
var options = new FindOneAndUpdateOptions<TopupRequest, TopupRequest>
{
IsUpsert = false,
ReturnDocument = ReturnDocument.After,
Sort = sort
};
var update = Builders<TopupRequest>.Update.Set("Status", TopupStatus.InProcessing);
var result = await topups.FindOneAndUpdateAsync(session, filter, update,options);
if (result == null)
session.AbortTransaction();
session.CommitTransaction();
return result;
}
catch (Exception e)
{
session.AbortTransaction();
Console.WriteLine(e);
return null;
}
}
I still had the same problem at the same time 1 record was used many times.
Please help me

inserting into SQL via sqlbulk

hallo i have this sniped code like this:
public static void Put_CSVtoSQL_Adhesion()
{
bool IsFirst = true;
DataTable dt = new DataTable();
string line = null;
int i = 0;
try
{
string fileName = Path.Combine(HttpContext.Current.Server.MapPath(UploadDirectory), TheFileName);
using (StreamReader sr = File.OpenText(fileName))
{
while ((line = sr.ReadLine()) != null)
{
string[] data = line.Split(';');
if (data.Length > 0)
{
if (i == 0)
{
foreach (var item in data)
{
dt.Columns.Add(new DataColumn());
}
i++;
}
DataRow row = dt.NewRow();
row.ItemArray = data;
// Pour enlever la tete
if (!IsFirst) dt.Rows.Add(row);
IsFirst = false;
}
}
}
using (var connectionWrapper = new Connexion())
{
var connectedConnection = connectionWrapper.GetConnected();
using (SqlBulkCopy copy = new SqlBulkCopy(connectionWrapper.conn))
{
int CountColl = dt.Columns.Count;
copy.ColumnMappings.Add(0, 1);
copy.ColumnMappings.Add(1, 2);
copy.ColumnMappings.Add(2, 3);
copy.ColumnMappings.Add(3, 4);
copy.ColumnMappings.Add(4, 5);
copy.DestinationTableName = "cotisation";
copy.WriteToServer(dt);
}
}
}
catch (Exception excThrown)
{
throw new Exception(excThrown.Message);
}
}
this code work well, but now i have more than 60 column, should i type manualy from 1 to 60 column or there are another methode ?
copy.ColumnMappings.Add(0, 1);
copy.ColumnMappings.Add(1, 2);
copy.ColumnMappings.Add(2, 3);
copy.ColumnMappings.Add(3, 4);
copy.ColumnMappings.Add(4, 5);
...until 60 column ?
the column is all the same i just shifted 1 column, because the first one is autoincremented column as an ID
Write a loop?
for (int i = 0; i < dt.Columns.Count - 1; i++)
{
copy.ColumnMappings.Add(i, i + 1);
}

Change label's text with using property

I am working on my C# ADO.NET app. I have connected my SQL Server database with C# app, and I can perform simple CRUD operations. I want to make that my app open my reminder form when someone in my database have birthday, so I made my query and all persons who have birthday on today's day are in my query, and with using property from my reminder form I change label's text with name and surname of person who have birthday. Now I just dont know how to change next label's text when more than one person have birthday in my query... I dont know how to get next element in my foreach loop...
Here is my code:
Form2 forma = new Form2();
TBirthDayEntities today_born = new TBirthDayEntities();
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
private Boolean provjera_rodj()
{
Boolean flag = false;
int cnt = 0;
IQueryable<TBD> query;
using (var data = new TBirthDayEntities())
{
query = (from x in data.TBD
where x.BirthDay.Day == System.DateTime.Now.Day && x.BirthDay.Month == System.DateTime.Now.Month
select x);
foreach (var x in query)
{
today_born.TBD.Add(x);
cnt += 1;
flag = true;
}
}
switch (cnt)
{
case 1:
{
foreach (var x in today_born.TBD)
{
forma.p_label2 = x.FName + " " + x.LName;
}
break;
}
case 2:
{
foreach (var x in today_born.TBD)
{
forma.p_label2 = x.FName + x.LName;
forma.p_label3 = x.FName + x.LName; //wrong
}
break;
}
}
return flag;
}
private void timer1_Tick(object sender, EventArgs e)
{
Boolean flag = provjera_rodj();
if (flag == true)
{
forma.Show();
timer1.Stop();
}
}
switch (cnt)
{
case 1:
case 2:
{
var lstLabel = new List<Label>()
{
forma.p_label2
, forma.p_label3
};
for(int i = 0; i < today_born.TBD.Count; i++)
{
var x in today_born.TBD[i];
lstLabel[x].Text = x.FName + x.LName;
}
break;
}
}
EDIT:
switch (cnt)
{
case 1:
case 2:
{
var lstLabel = new List<Action<string>>()
{
new Action<string>(s =>forma.p_label2 = s)
, new Action<string>(s =>forma.p_label3 = s)
};
for(int i = 0; i < today_born.TBD.Count; i++)
{
var x = today_born.TBD[i];
lstLabel[x](x.FName + x.LName);
}
break;
}
}

excel data uploading is not working in sql database

This is pradeep
This is the code of the excel uploading to sql database
protected void btnupload_Click ( object sender, EventArgs e )
{
//string name = ddloutlet.SelectedValue.ToString ();
//cal
try
{
System.IO.FileInfo file = new System.IO.FileInfo(fileupload1.PostedFile.FileName);
string fname = file.Name.Remove((file.Name.Length - file.Extension.Length), file.Extension.Length);
fname = fname + DateTime.Now.ToString("_ddMMyyyy_HHmmss") + file.Extension;
fileupload1.PostedFile.SaveAs(Server.MapPath("locations/") + fname);
string filexetion = file.Extension;
if ( filexetion == ".xlsx" )
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Server.MapPath ( "locations/" ) + fname + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
}
else if ( filexetion == ".xls" )
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath ( "locations/" ) + fname + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes; \"";
}
OleDbConnection connection = new OleDbConnection(excelConnectionString);
OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection);
connection.Open();
OleDbDataReader dr = command.ExecuteReader();
SqlConnection conn = new SqlConnection(strconnection);
conn.Open();
try
{
if (dr.Read() == true)
{
while (dr.Read())
{
string locationname = dr["Location Name"].ToString();
string status = dr["Status"].ToString();
if (locationname != "" && status != "")
{
string query = " select locationname from tbllocations where locationname='" + locationname + "' and outletid='" + Session["outlet_id"].ToString() + "'";
// conn.Open();
SqlCommand cmdquery = new SqlCommand(query, conn);
SqlDataReader drreader;
drreader = cmdquery.ExecuteReader();
if (drreader.Read())
{
c = true;
ssss = ssss + locationname + ",";
// ss = ssss.Split(',');
}
else
{
drreader.Close();
string qryprduct = "insert into tbllocations(locationname,status,outletid,cityid)values('" + locationname + "','" + status + "','" + Session["outlet_id"].ToString() + "','" + Session["cityid"].ToString() + "')";
SqlCommand cmd1 = new SqlCommand(qryprduct, conn);
conn.Close();
conn.Open();
cmd1.ExecuteNonQuery();
lblerror1.Visible = true;
lblerror1.Text = "Locations uploaded Sucess";
//conn.Close();
}
drreader.Close();
}
}
// connection.Close (); conn.Close ();
}
else
{
lblerror1.Text = "There is a empty excel sheet file,plz check";
lblerror1.Visible = true;
}
}
catch (Exception ex)
{
lblerror1.Visible = true;
lblerror1.Text = "Plz check the excel file formate";
}
finally
{
connection.Close(); conn.Close();
bind();
if (c == true)
{
lblerror1.Visible = true;
lblerror1.Text = "In excel this loactions are already exist. Please check,";
//for (int i = 0; i < ss.Length; i++)
//{
lblerror3.Visible = true;
lblerror3.Text = ssss;
//}
}
}
}
catch
{
}
}
The above code uploading is working but in excel 1st record is not uploading ,please tell me the what is the problem and give me suggestion please.
excel data is
Location Name Status
test1 1
test2 1
test3 1
test4 0
test5 1
test6 0
test7 1
test8 0
test9 1
test10 1
Thanks
Pradeep
You need to remove the
if (dr.Read() == true)
because it is immediately followed by a
while (dr.Read())
Each of these will read a record and the first one will skip the first row of the file