求助大神:无法找到表0。

Ivy_Liu_zw 2016-10-22 09:32:47
//错误信息:未处理 System.IndexOutOfRangeException
HResult=-2146233080
Message=无法找到表 0。
Source=System.Data
StackTrace:
在 System.Data.DataTableCollection.get_Item(Int32 index)
在 WindowsFormsApplication1.Form1.button6_Click(Object sender, EventArgs e) 位置 C:\Users\Z\Documents\Visual Studio 2010\Projects\wp1 public class\WindowsFormsApplication1\Form1.cs:行号 36
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 WindowsFormsApplication1.Program.Main() 位置 C:\Users\Z\Documents\Visual Studio 2010\Projects\wp1 public class\WindowsFormsApplication1\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:


//创建一个全局类
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;


namespace Acess
{
public class GlobalValue
{
public static OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=shujuku.accdb");
public static OleDbDataAdapter da = new OleDbDataAdapter("select * from 控制点", conn);
//public static OleDbDataAdapter da;
public static DataSet ds = new DataSet();
public static DataSet queryDs = new DataSet();
public static OleDbCommandBuilder cmd = new OleDbCommandBuilder(da);

//public static void UpDataSrc()
//{
// conn.Open();
// da = new OleDbDataAdapter("select * from student_info", conn);
// da.InsertCommand = GlobalValue.cmd.GetInsertCommand();
// da.UpdateCommand = GlobalValue.cmd.GetUpdateCommand();
// da.DeleteCommand = GlobalValue.cmd.GetDeleteCommand();
// da = new OleDbDataAdapter("select * from student_info", conn);
// //da.Fill(GlobalValue.ds);
// conn.Close();
//}
//public static void InitGloVa()
//{
// conn.Open();
// da = new OleDbDataAdapter("select * from student_info", conn);
// da.InsertCommand = GlobalValue.cmd.GetInsertCommand();
// da.UpdateCommand = GlobalValue.cmd.GetUpdateCommand();
// da.DeleteCommand = GlobalValue.cmd.GetDeleteCommand();
// da = new OleDbDataAdapter("select * from student_info", conn);
// da.Fill(GlobalValue.ds);
// conn.Close();
//}
}
}
//
//
//Form1中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button6_Click(object sender, EventArgs e)
{
try
{

//string strFilter = "Sno = " + textBox1.Text;
//DataView dv = GlobalValue.ds.Tables[0].DefaultView;
//dv.RowFilter = strFilter;
if (knownname.Text == string.Empty)
{
MessageBox.Show("点名不能为空!");

return;
}
object key = knownname.Text;
DataRow dr = Acess.GlobalValue.ds.Tables[0].Rows.Find(key);
//运行到这一行开始出错

if (dr != null)
{
MessageBox.Show("已有该点名!");
this.Close();
//GlobalValue.conn.Close();
return;
}
Acess.GlobalValue.conn.Open();//打开数据库,建立连接

DataRow drx = Acess.GlobalValue.ds.Tables[0].NewRow(); //创建一条新记录行
drx["点名"] = knownname.Text;
drx["X坐标(m)"] = Convert.ToInt32(knownx.Text);
drx["Y坐标(m)"] = Convert.ToInt32(knowny.Text);
drx["高程(m)"] = Convert.ToInt32(knownh.Text);
Acess.GlobalValue.ds.Tables[0].Rows.Add(drx); //在表中追加记录

int i = Acess.GlobalValue.da.Update(Acess.GlobalValue.ds); //更新数据库
if (i > 0)
MessageBox.Show("插入成功!");
else
MessageBox.Show("插入失败!");

Acess.GlobalValue.conn.Close();//最后 关闭 数据库 连接
this.Close();
}
finally
{
}
//catch (Exception a)
//{
// throw a;
//}

}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“shujukuDataSet.控制点”中。您可以根据需要移动或删除它。
this.控制点TableAdapter.Fill(this.shujukuDataSet.控制点);
try
{
Acess.GlobalValue.conn.Open();
Acess.GlobalValue.da = new OleDbDataAdapter("select * from 控制点", Acess.GlobalValue.conn);
Acess.GlobalValue.da.InsertCommand = Acess.GlobalValue.cmd.GetInsertCommand();
Acess.GlobalValue.da.UpdateCommand = Acess.GlobalValue.cmd.GetUpdateCommand();
Acess.GlobalValue.da.DeleteCommand = Acess.GlobalValue.cmd.GetDeleteCommand();
Acess.GlobalValue.da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
Acess.GlobalValue.da.Fill(Acess.GlobalValue.ds);
Acess.GlobalValue.conn.Close();
dataGridView1.DataSource = Acess.GlobalValue.ds.Tables[0];
}
catch (System.Exception ex)
{
throw ex;
}
}
}
}
...全文
615 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2016-10-22
  • 打赏
  • 举报
回复
tables["表名"]指定是否正确?
汉软 2016-10-22
  • 打赏
  • 举报
回复
你吧数据库文件放到D盘根目录,修改连接字符串这一行为: public static OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\shujuku.accdb"); 然后再试试看。
Ivy_Liu_zw 2016-10-22
  • 打赏
  • 举报
回复
请问要怎么修改,我是刚接触C#的菜鸟,帮帮忙,谢谢!
mygame0302 2016-10-22
  • 打赏
  • 举报
回复
Acess.GlobalValue.da.Fill(Acess.GlobalValue.ds); 执行后 Acess.GlobalValue.ds并未获取到数据吧

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧