I have a dataset and I'm adding new rows, and then I'm updating the database behind the dataset using the following command:
DataSetReasons ds = new DataSetReasons();
DataSetReasonsTableAdapters.Data_Tracker_RcodeTableAdapter dta = new DataSetReasonsTableAdapters.Data_Tracker_RcodeTableAdapter();
DataSetReasons.Data_Tracker_RcodeDataTable GRX =
new DataSetReasons.Data_Tracker_RcodeDataTable();
DataRow rowx = GRX.NewRow();
rowx[0] = 111;
rowx[1] = 28;
rowx[2] = "C";
rowx[3] = 12;
rowx[4] = "C";
rowx[5] = 16;
rowx[6] = TextBox2.Text;
GRX.Rows.Add(rowx); //<--- adding the row
dta.Update(GRX); //<-- updating the DB
now everything works fine, except that I want to put the update command in a separate button. when I do so, the DB update are not happening.
any idea?
Solved, I was missing the "static" word before defining the data table.
Many thanks to whoever passed on this question.
Related
A restore performed with C# code using SMO objects restores fine when original database is in SIMPLE recovery mode. We got problems with one database where the restore missed all content from a certain table, where all data was inserted late in process. The database showed to be in BULK LOGGED recovery mode. After changing to SIMPLE and doing a new backup, it restored fine, using our code.
We have tried different settings on the restore object, but found none that fixes the problem. We are under the impression that the restoring ignores data in the log.
The basic restore looks like this:
sqlServer = new Server(new ServerConnection(instanceName));
restore = GetRestore();
restore.PercentComplete += PercentCompleteAction;
restore.Complete += CompleteAction;
restore.SqlRestore(sqlServer);
the GetRestore function is basically implemented like this:
restore = new Restore();
var deviceItem = new BackupDeviceItem(backupFileName, DeviceType.File);
restore.Devices.Add(deviceItem);
restore.Database = newDatabaseName;
restore.NoRecovery = false;
restore.Action = RestoreActionType.Database;
restore.ReplaceDatabase = false;
return restore;
There are no error messages - just missing content in one table.
Added try:
I took a guess at the solution below, but it didn't help:
restore.ReplaceDatabase = false;
restore.NoRecovery = true;
restore.Action = RestoreActionType.Database;
restore.SqlRestore(sqlServer);
restore.ReplaceDatabase = true;
restore.NoRecovery = true;
restore.Action = RestoreActionType.Log;
restore.SqlRestore(sqlServer);
restore.ReplaceDatabase = true;
restore.NoRecovery = false;
restore.Action = RestoreActionType.Files;
restore.SqlRestore(sqlServer);
You need select a correct FileNumber in your Log file, see this method can solve your problem:
public static void restaurarBackup(string pathFileBak)
{
SqlConnection conn = new SqlConnection(Connection.getCon());
Server smoServer = new Server(new ServerConnection(conn));
string localFilePath = pathFileBak;
string db_name = "ETrade";
string defaultFolderEtrade = #"C:\ETrade\";
Restore rs = new Restore();
rs.NoRecovery = false;
rs.ReplaceDatabase = true;
BackupDeviceItem bdi = default(BackupDeviceItem);
bdi = new BackupDeviceItem(localFilePath, DeviceType.File);
rs.Devices.Add(bdi);
DataTable dt = rs.ReadFileList(smoServer);
foreach (DataRow r in dt.Rows)
{
string logicalFilename = r.ItemArray[dt.Columns["LogicalName"].Ordinal].ToString();
string physicalFilename = defaultFolderEtrade + Path.GetFileName(r.ItemArray[dt.Columns["PhysicalName"].Ordinal].ToString());
rs.RelocateFiles.Add(new RelocateFile(logicalFilename, physicalFilename));
}
DataTable backupHeaders = rs.ReadBackupHeader(smoServer);
rs.FileNumber = Convert.ToInt32(backupHeaders.AsEnumerable().Max(backupInfo => backupInfo["Position"]));
rs.Database = db_name;
smoServer.KillAllProcesses(rs.Database);
Microsoft.SqlServer.Management.Smo.Database db = smoServer.Databases[rs.Database];
db.DatabaseOptions.UserAccess = DatabaseUserAccess.Single;
rs.SqlRestore(smoServer);
db = smoServer.Databases[rs.Database];
db.SetOnline();
smoServer.Refresh();
db.Refresh();
}
If you want look backups in your Log file, see this query:
SELECT database_name, name, backup_start_date,
backup_finish_date, datediff(mi, backup_start_date, backup_finish_date) [tempo (min)],
position, first_lsn, last_lsn, server_name, recovery_model,
type, cast(backup_size/1024/1024 as numeric(15,2)) [Tamanho (MB)], B.is_copy_only
FROM msdb.dbo.backupset B
I am trying to make an adding form with DevExpress. In my form, there is a lookUpEdit object and I am trying to fill it with columns coming from a dataset. So far, I wrote this:
this.companylookUpEdit.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.incomesexpensesBindingSource, "company_id", true));
this.companylookUpEdit.EditValue = 1;
this.companylookUpEdit.Location = new System.Drawing.Point(100, 12);
this.companylookUpEdit.Name = "companylookUpEdit";
this.companylookUpEdit.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
this.companylookUpEdit.Properties.DataSource = this.companyBindingSource;
this.companylookUpEdit.Properties.DisplayMember = "name";
this.companylookUpEdit.Properties.ValueMember = "id";
this.companylookUpEdit.Size = new System.Drawing.Size(209, 20);
this.companylookUpEdit.TabIndex = 22;
I want to show company name coming from companyBindingSource, and push corresponding company id to another dataset as an attribute.
When I run the application, the form appears, but when I click to the down arrow, nothing happens. Can anyone help me about this?
I would say I'm new to VB but I have been using it for over a year although for mainly small tasks (mostly school related). Anyway my current project is for my A-Level task and I need to be able to add, update, delete and read from a database. My current code allows this to happen but after a new record is added to my database the dataset in VB displays the rows differently. I have 3 tables: "PlayerInfo", "PlayerSkill" and "PlayerAbilities". when a new record is added; the new "PlayerInfo" information becomes the first row in my dataset while the "PlayerSkill" and "PlayerAbilities" become the last row. This causes all of the information to be improperly matched up. I was wondering if anyone else has had this problem and if they know how to solve it.
This shows the working code to add to my database.
If inc <> -1 Then
Dim cb1 As New OleDb.OleDbCommandBuilder(da1)
Dim cb2 As New OleDb.OleDbCommandBuilder(da2)
Dim cb3 As New OleDb.OleDbCommandBuilder(da3)
Dim dsNewRow1 As DataRow
Dim dsNewRow2 As DataRow
Dim dsNewRow3 As DataRow
Try
dsNewRow1 = ds1.Tables("Players").NewRow()
dsNewRow1.Item("Forename") = Forename.Text()
dsNewRow1.Item("Surname") = Surname.Text()
ds1.Tables("Players").Rows.Add(dsNewRow1)
da1.Update(ds1, "Players")
dsNewRow3 = ds3.Tables("Players").NewRow()
dsNewRow3.Item("Reactions") = Reactions.Text()
dsNewRow3.Item("Strength") = Strength.Text()
dsNewRow3.Item("Speed") = Speed.Text()
dsNewRow3.Item("Stamina") = Stamina.Text()
dsNewRow3.Item("Accuracy") = Accuracy.Text()
dsNewRow3.Item("Coordination") = Coordination.Text()
ds3.Tables("Players").Rows.Add(dsNewRow3)
da3.Update(ds3, "Players")
dsNewRow2 = ds2.Tables("Players").NewRow()
dsNewRow2.Item("RegularShot") = RegularShot.Text()
dsNewRow2.Item("ShortServe") = ShortServe.Text()
dsNewRow2.Item("FlickServe") = FlickServe.Text()
dsNewRow2.Item("Clear") = Clear.Text()
dsNewRow2.Item("Smash") = Smash.Text()
dsNewRow2.Item("DropShot") = DropShot.Text()
ds2.Tables("Players").Rows.Add(dsNewRow2)
da2.Update(ds2, "Players")
MsgBox("New Record added to the Database")
Commit.Enabled = False
AddNew.Enabled = True
Update.Enabled = True
Delete.Enabled = True
Catch
MsgBox("Error")
Me.Close()
End Try
End If
This shows the code that displays each of the dataset records inside different text boxes.
Try
Forename.Text = ds1.Tables("Players").Rows(inc).Item(1)
Surname.Text = ds1.Tables("Players").Rows(inc).Item(2)
Speed.Text = ds3.Tables("Players").Rows(inc).Item(3)
Strength.Text = ds3.Tables("Players").Rows(inc).Item(2)
Reactions.Text = ds3.Tables("Players").Rows(inc).Item(1)
Stamina.Text = ds3.Tables("Players").Rows(inc).Item(4)
Coordination.Text = ds3.Tables("Players").Rows(inc).Item(6)
Accuracy.Text = ds3.Tables("Players").Rows(inc).Item(5)
ShortServe.Text = ds2.Tables("Players").Rows(inc).Item(2)
FlickServe.Text = ds2.Tables("Players").Rows(inc).Item(3)
Clear.Text = ds2.Tables("Players").Rows(inc).Item(4)
Smash.Text = ds2.Tables("Players").Rows(inc).Item(5)
DropShot.Text = ds2.Tables("Players").Rows(inc).Item(6)
RegularShot.Text = ds2.Tables("Players").Rows(inc).Item(1)
Catch
MsgBox("Error")
Me.Close()
End Try
Sadly I do not currently have enough "Rep" to upload a picture of the dataset.
Also sorry if this is not enough information, I'm not really used to using forums and such.
I'm not entirely sure if this counts as an answer as I still don't know what caused the problem in the first case. Anyway I've got my code doing what I wanted by editing the SQL statements to display the rows in order of the primary key. ORDER BY [Player#]. I did have to change the name to just "Player" because the "CommandBuilder" doesn't like special characters.
I am inserting data in the sql server.Here is the code:
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter();
param[0].ParameterName = "#Exch";
param[0].SqlDbType = SqlDbType.Int;
param[0].Value = "BSE";
formObjSqlConnection.SQLConnection.Executes("LPExch..DeleteVarMargin", param);
foreach (BseVar objBseVar in objListNseData)
{
currentIndex++;
param = new SqlParameter[10];
param[0] = new SqlParameter();
param[0].ParameterName = "#Exch";
param[0].SqlDbType = SqlDbType.Char;
param[0].Value = "BSE";
param[1] = new SqlParameter();
param[1].ParameterName = "#Symbol";
param[1].SqlDbType = SqlDbType.Char;
param[1].Size = 10;
param[1].Value = objBseVar.Symbol;
param[2] = new SqlParameter();
param[2].ParameterName = "#Series";
param[2].SqlDbType = SqlDbType.Char;
param[2].Value = "EQ";
param[3] = new SqlParameter();
param[3].ParameterName = "#SecurityVar";
param[3].SqlDbType = SqlDbType.SmallMoney;
param[3].Value = objBseVar.SecurityVar;
param[4] = new SqlParameter();
param[4].ParameterName = "#IndexVar";
param[4].SqlDbType = SqlDbType.SmallMoney;
param[4].Value = 0;
param[5] = new SqlParameter();
param[5].ParameterName = "#VarMargin";
param[5].SqlDbType = SqlDbType.SmallMoney;
param[5].Value = objBseVar.IndexVar;
param[6] = new SqlParameter();
param[6].ParameterName = "#AdhocMargin";
param[6].SqlDbType = SqlDbType.SmallMoney;
param[6].Value = objBseVar.SecurityVar - objBseVar.IndexVar;
param[7] = new SqlParameter();
param[7].ParameterName = "#VarMarginRate";
param[7].SqlDbType = SqlDbType.SmallMoney;
param[7].Value = objBseVar.IndexVar;
formObjSqlConnection.SQLConnection.Executes("LPExch..UpdateOrAddCashVarMarginBSE", param);
if (Convert.ToInt32(1 / progressChange * currentIndex) <= 100)
objImportMaster.UpdateProgressBar(Convert.ToInt32(1 / progressChange * currentIndex));
}
formObjSqlConnection.SQLConnection.Executes("LPExch..UpdateCashVarMarginBSEScripNo");
Is there any other way i can insert the data in the database.The problem is that database is taking too much time to insert the data.Also i am updating the progressbar in other UI.
Is that the reason for slow insertion?
Can we use any other way to insert data.?
Right now you are sending every insert one at a time. That's slow. If you can change the database, look up table valued parameters and pass a table to your function so you can send all the records just once.
If you have direct access to a table, you can also use bulk copy.
If you are trying to insert many records then consider using bulk copy, it is quite fast.
An example is available here.
I want to remove some special characters in a table of database. I've used strong typed table to do it. When i got all data into dataset from database and modified it then i called method update() of data adapter to turn dataset to database but it doesn't work.
Below is my code
DsTel tel = new DsTel();
DsTelTableAdapters.telephone_bkTableAdapter adapter = new DsTelTableAdapters.telephone_bkTableAdapter();
adapter.Connection = new SqlConnection(ConfigurationManager.AppSettings["SiteSqlServer"].ToString());
adapter.Fill(tel.telephone_bk);
foreach (DsTel.telephone_bkRow row in tel.telephone_bk.Rows)
{
row.telephoneNo = RemoveWhiteSpace(row.telephoneNo.ToString());
row.AcceptChanges();
}
tel.AcceptChanges();
adapter.Update(tel.telephone_bk);
Please give me some ideas?
Thanks in advance.
I've found the solution for this problem by using the TableAdapterManager.
Below is my code:
DsTel tel = new DsTel();
DsTelTableAdapters.telephone_bkTableAdapter adapter = new DsTelTableAdapters.telephone_bkTableAdapter();
adapter.Connection = new SqlConnection(ConfigurationManager.AppSettings["SiteSqlServer"].ToString());
adapter.Fill(tel.telephone_bk);
foreach (DsTel.telephone_bkRow row in tel.telephone_bk.Rows)
{
if (!row.IstelephoneNoNull())
{
row.telephoneNo = RemoveWhiteSpace(row.telephoneNo.ToString());
}
}
DsTelTableAdapters.TableAdapterManager mrg = new DsTelTableAdapters.TableAdapterManager();
mrg.telephone_bkTableAdapter = adapter;
mrg.BackupDataSetBeforeUpdate = true;
mrg.UpdateAll((DsTel)tel.GetChanges());
You've called AcceptChanges before the update. This means the dataset has no changes any more so there is nothing to send to the database.
Remove all of the calls to AcceptChanges and add one AFTER the update. ie:
DsTel tel = new DsTel();
DsTelTableAdapters.telephone_bkTableAdapter adapter = new DsTelTableAdapters.telephone_bkTableAdapter();
adapter.Connection = new SqlConnection(ConfigurationManager.AppSettings["SiteSqlServer"].ToString());
adapter.Fill(tel.telephone_bk);
foreach (DsTel.telephone_bkRow row in tel.telephone_bk.Rows)
{
row.telephoneNo = RemoveWhiteSpace(row.telephoneNo.ToString());
}
adapter.Update(tel.telephone_bk);
tel.telephone_bk.AcceptChanges();