记录不会添加到我的表中

编程入门 行业动态 更新时间:2024-10-11 13:22:49
本文介绍了记录不会添加到我的表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我试图运行以下代码时写的代码: www.homeandlearn.co.uk/csharp/csharp_s12p10.html 我发现记录添加到屏幕显示不会添加到表格。 这是代码:

使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用 System.Data; 使用 System.Drawing; 使用 System.Linq; 使用 System.Text; 使用 System.Threading.Tasks; 使用 System.Windows.Forms; 命名空间 _46_creating_databse_01_sql_express_02 { public partial class Form1:Form { public Form1(){InitializeComponent(); } DatabaseConnection objConnect; string conString; DataSet ds; DataRow dRow; int MaxRows; int inc = 0 ; private void Form1_Load_1(对象发​​件人,EventArgs e) { 尝试 { objConnect = new DatabaseConnection(); conString = Properties.Settings.Default.employeesConnectionString; objConnect.connection_string = conString; objConnect.Sql = Properties.Settings.Default.sql; ds = objConnect.GetConnection; MaxRows = ds.Tables [ 0 ]。Rows.Count; NavigateRecords(); } catch (异常错误) { MessageBox.Show(err.Message); } } 私有 void NavigateRecords( ) { dRow = ds.Tables [ 0 ]。行[inc]; txtFirstName.Text = dRow.ItemArray.GetValue( 1 )。ToString(); txtSurname.Text = dRow.ItemArray.GetValue( 2 )。ToString(); txtJobTitle.Text = dRow.ItemArray.GetValue( 3 )。ToString(); txtDepartment.Text = dRow.ItemArray.GetValue( 4 )。ToString(); labelUpdate(); } private void labelUpdate() { label6.Text = 记录 +(inc + 1 )+ of + MaxRows; } private void button2_Click( object sender,EventArgs e) { if (inc!= MaxRows - 1 ) { inc ++; NavigateRecords(); } else { MessageBox.Show( 没有更多行); } } private void button3_Click( object sender,EventArgs e) { if (inc > 0 ) { inc--; NavigateRecords(); } else { MessageBox.Show( first record); } } private void button4_Click( object sender,EventArgs e) { if (inc!= 0 ) { inc = 0 ; NavigateRecords(); } } private void button1_Click( object sender,EventArgs e) { if (inc!= MaxRows - 1 ) { inc = MaxRows - 1 ; NavigateRecords(); } } 私有 void button6_Click( object sender,EventArgs e) { txtFirstName.Clear(); txtSurname.Clear(); txtJobTitle.Clear(); txtDepartment.Clear(); btnAddNew.Enabled = false ; btnSave.Enabled = true ; btnCancel.Enabled = true ; } private void btnCancel_Click( object sender,EventArgs e) { NavigateRecords(); btnCancel.Enabled = false ; btnSave.Enabled = false ; btnAddNew.Enabled = true ; } private void button7_Click( object sender,EventArgs e) { DataRow row = ds.Tables [ 0 ]。NewRow( ); row [ 1 ] = txtFirstName.Text; 行[ 2 ] = txtSurname.Text; row [ 3 ] = txtJobTitle.Text; row [ 4 ] = txtDepartment.Text; ds.Tables [ 0 ]。Rows.Add(row); 尝试 { objConnect.UpdateDatabase(ds); MaxRows = MaxRows + 1 ; inc = MaxRows - 1 ; MessageBox.Show( database updated); } catch (异常错误) { MessageBox.Show(err.Message); } btnCancel.Enabled = false ; btnSave.Enabled = false ; btnAddNew.Enabled = true ; } private void button9_Click( object sender,EventArgs e) { DataRow row = ds.Tables [ 0 ]行[INC]。 row [ 1 ] = txtFirstName.Text; 行[ 2 ] = txtSurname.Text; row [ 3 ] = txtJobTitle.Text; row [ 4 ] = txtDepartment.Text; 尝试 { objConnect.UpdateDatabase(ds); MessageBox.Show( Record Updated); } catch (异常错误) { MessageBox.Show(err.Message); } } } }

解决方案

When I am trying to run this code written in : www.homeandlearn.co.uk/csharp/csharp_s12p10.html I find that records are added to the screen display are not added to the table . Here is the code :

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _46_creating_databse_01_sql_express_02 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DatabaseConnection objConnect; string conString; DataSet ds; DataRow dRow; int MaxRows; int inc = 0; private void Form1_Load_1(object sender, EventArgs e) { try { objConnect = new DatabaseConnection(); conString = Properties.Settings.Default.employeesConnectionString; objConnect.connection_string = conString; objConnect.Sql = Properties.Settings.Default.sql; ds = objConnect.GetConnection; MaxRows = ds.Tables[0].Rows.Count; NavigateRecords(); } catch (Exception err) { MessageBox.Show(err.Message); } } private void NavigateRecords() { dRow = ds.Tables[0].Rows[inc]; txtFirstName.Text = dRow.ItemArray.GetValue(1).ToString(); txtSurname.Text = dRow.ItemArray.GetValue(2).ToString(); txtJobTitle.Text = dRow.ItemArray.GetValue(3).ToString(); txtDepartment.Text = dRow.ItemArray.GetValue(4).ToString(); labelUpdate(); } private void labelUpdate() { label6.Text = "Record " + (inc + 1) + " of " + MaxRows; } private void button2_Click(object sender, EventArgs e) { if (inc != MaxRows - 1) { inc++; NavigateRecords(); } else { MessageBox.Show("No More Rows"); } } private void button3_Click(object sender, EventArgs e) { if (inc > 0) { inc--; NavigateRecords(); } else { MessageBox.Show("first Record"); } } private void button4_Click(object sender, EventArgs e) { if (inc != 0) { inc = 0; NavigateRecords(); } } private void button1_Click(object sender, EventArgs e) { if (inc != MaxRows - 1) { inc = MaxRows - 1; NavigateRecords(); } } private void button6_Click(object sender, EventArgs e) { txtFirstName.Clear(); txtSurname.Clear(); txtJobTitle.Clear(); txtDepartment.Clear(); btnAddNew.Enabled = false; btnSave.Enabled = true; btnCancel.Enabled = true; } private void btnCancel_Click(object sender, EventArgs e) { NavigateRecords(); btnCancel.Enabled = false; btnSave.Enabled = false; btnAddNew.Enabled = true; } private void button7_Click(object sender, EventArgs e) { DataRow row = ds.Tables[0].NewRow(); row[1] = txtFirstName.Text; row[2] = txtSurname.Text; row[3] = txtJobTitle.Text; row[4] = txtDepartment.Text; ds.Tables[0].Rows.Add(row); try { objConnect.UpdateDatabase(ds); MaxRows = MaxRows + 1; inc = MaxRows - 1; MessageBox.Show("database updated"); } catch (Exception err) { MessageBox.Show(err.Message); } btnCancel.Enabled = false; btnSave.Enabled = false; btnAddNew.Enabled = true; } private void button9_Click(object sender, EventArgs e) { DataRow row = ds.Tables[0].Rows[inc]; row[1] = txtFirstName.Text; row[2] = txtSurname.Text; row[3] = txtJobTitle.Text; row[4] = txtDepartment.Text; try { objConnect.UpdateDatabase(ds); MessageBox.Show("Record Updated"); } catch (Exception err) { MessageBox.Show(err.Message); } } } }

解决方案

更多推荐

记录不会添加到我的表中

本文发布于:2023-10-30 04:54:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!