Gerar arquivo DBF de uma tabela SQL Server
Boa tarde.
Tenho uma tabela no SQL Server que preciso converter em formato DBF.
Utilizando o Visual Studio 2017, criei um Windows Form Application e consigo ler a tabela e carregar os dados num DataGridView.
Esses são as 3 primeiras linhas da minha tabela.
1 ATIVO T N
1.1 ATIVO CIRCULANTE T N
1.1.1 CAIXA E EQUIVALENTES DE CAIXA T N
A dificuldade está na geração para o DBF. Instalei o FoxPro9.0 mas ao executar meu código, retorna o erro:
System.Data.OleDb.OleDbException
HResult=0x80004005
Message=Variable ''''ATIVO'''' is not found.
Source=System.Data
Mas como podem ver, ''''ATIVO'''' é o valor de uma coluna da tabela. Não consegui descobrir meu erro.
private void button2_Click(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
string connectionString = @"Provider=VFPOLEDB.1;Data Source=C:\\\\\\\\TOTVS";
OleDbConnection connection = new OleDbConnection(connectionString);
using (OleDbCommand command = connection.CreateCommand())
{
connection.Open();
OleDbParameter script = new OleDbParameter("script", @"CREATE TABLE planocontas ([CD_CLASSIF] varchar(128),[DS_CONTA] varchar(128),[IE_TIPO] varchar(128),[IE_CENTRO] varchar(128))");
if ((System.IO.File.Exists(@"c:\\\\\\\\TOTVS\\\\\\\\planocontas.dbf")))
{
System.IO.File.Delete(@"c:\\\\\\\\TOTVS\\\\\\\\planocontas.dbf");
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "ExecScript";
command.Parameters.Add(script);
command.ExecuteNonQuery();
}
else
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "ExecScript";
command.Parameters.Add(script);
command.ExecuteNonQuery();
}
connection.Dispose();
}
OleDbConnection connection1 = new OleDbConnection(connectionString);
using (OleDbCommand scriptCommand = connection1.CreateCommand())
{
connection1.Open();
DataTable dt = (DataTable)(bs.DataSource);
foreach (DataRow dr in dt.Rows)
{
string CD_CLASSIF = dr["CD_CLASSIF"].ToString();
string DS_CONTA = dr["DS_CONTA"].ToString();
string IE_TIPO = dr["IE_TIPO"].ToString();
string IE_CENTRO = dr["IE_CENTRO"].ToString();
scriptCommand.CommandType = CommandType.StoredProcedure;
scriptCommand.CommandText = "ExecScript";
scriptCommand.Parameters.Add("myScript", OleDbType.Char).Value = @"INSERT INTO PLANOCONTAS ([CD_CLASSIF],[DS_CONTA],[IE_TIPO],[IE_CENTRO]) VALUES (" + CD_CLASSIF + "," + DS_CONTA + "," + IE_TIPO + "," + IE_CENTRO + ")";
scriptCommand.ExecuteNonQuery();
}
}
Tenho uma tabela no SQL Server que preciso converter em formato DBF.
Utilizando o Visual Studio 2017, criei um Windows Form Application e consigo ler a tabela e carregar os dados num DataGridView.
Esses são as 3 primeiras linhas da minha tabela.
1 ATIVO T N
1.1 ATIVO CIRCULANTE T N
1.1.1 CAIXA E EQUIVALENTES DE CAIXA T N
A dificuldade está na geração para o DBF. Instalei o FoxPro9.0 mas ao executar meu código, retorna o erro:
System.Data.OleDb.OleDbException
HResult=0x80004005
Message=Variable ''''ATIVO'''' is not found.
Source=System.Data
Mas como podem ver, ''''ATIVO'''' é o valor de uma coluna da tabela. Não consegui descobrir meu erro.
private void button2_Click(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
string connectionString = @"Provider=VFPOLEDB.1;Data Source=C:\\\\\\\\TOTVS";
OleDbConnection connection = new OleDbConnection(connectionString);
using (OleDbCommand command = connection.CreateCommand())
{
connection.Open();
OleDbParameter script = new OleDbParameter("script", @"CREATE TABLE planocontas ([CD_CLASSIF] varchar(128),[DS_CONTA] varchar(128),[IE_TIPO] varchar(128),[IE_CENTRO] varchar(128))");
if ((System.IO.File.Exists(@"c:\\\\\\\\TOTVS\\\\\\\\planocontas.dbf")))
{
System.IO.File.Delete(@"c:\\\\\\\\TOTVS\\\\\\\\planocontas.dbf");
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "ExecScript";
command.Parameters.Add(script);
command.ExecuteNonQuery();
}
else
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "ExecScript";
command.Parameters.Add(script);
command.ExecuteNonQuery();
}
connection.Dispose();
}
OleDbConnection connection1 = new OleDbConnection(connectionString);
using (OleDbCommand scriptCommand = connection1.CreateCommand())
{
connection1.Open();
DataTable dt = (DataTable)(bs.DataSource);
foreach (DataRow dr in dt.Rows)
{
string CD_CLASSIF = dr["CD_CLASSIF"].ToString();
string DS_CONTA = dr["DS_CONTA"].ToString();
string IE_TIPO = dr["IE_TIPO"].ToString();
string IE_CENTRO = dr["IE_CENTRO"].ToString();
scriptCommand.CommandType = CommandType.StoredProcedure;
scriptCommand.CommandText = "ExecScript";
scriptCommand.Parameters.Add("myScript", OleDbType.Char).Value = @"INSERT INTO PLANOCONTAS ([CD_CLASSIF],[DS_CONTA],[IE_TIPO],[IE_CENTRO]) VALUES (" + CD_CLASSIF + "," + DS_CONTA + "," + IE_TIPO + "," + IE_CENTRO + ")";
scriptCommand.ExecuteNonQuery();
}
}
André Luis
Curtidas 0