当前位置: 代码迷 >> C# >> C# 无法找到表 0.的有关问题
  详细解决方案

C# 无法找到表 0.的有关问题

热度:403   发布时间:2016-05-05 02:56:05.0
C# 无法找到表 0.的问题
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Data.OracleClient;

public partial class chaxun : System.Web.UI.Page
{
    private string OracleconnectionString = "这里有连接数据库的字符串,没有错误";
    private OracleConnection oraclecon;

    private string SqlconnectionString = "这里有连接数据库的字符串,没有错误";
    private SqlConnection sqlcon;

    private DataSet ds = new DataSet();
    private DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] != null) ;    //用户可以访问。
        else
            Response.Redirect("default.aspx");//用户不能访问,扔回到登录页面:Response.Redirect("login.aspx");

        if (!IsPostBack)
        {
            Oraclebind();
        }
    }
   
    //编号查询ora
    protected void queding_Click(object sender, EventArgs e)
    {
        string BIANHAO = bianhao.Text;

        if (BIANHAO == "")
        {
            MessageBox.Show("编号不能为空!");
            return;
        }

        this.oraclecon = new OracleConnection(OracleconnectionString);
        string strsql = "SELECT * FROM XDFRYXX WHERE ID = '" + bianhao.Text + "'";
        OracleCommand com = new OracleCommand(strsql, this.oraclecon);
        OracleDataAdapter da = new OracleDataAdapter();
        da.SelectCommand = com;
        da.Fill(ds);

        int nCount = ds.Tables[0].Rows.Count;
        // 编码不存在            
        if (nCount == 0)
        {
            MessageBox.Show("您输入的编号不存在,请重新输入");
            return;
        }
        // 编码存在时            
        else
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    //取消
    protected void quxiao_Click(object sender, EventArgs e)
    {
        Response.Redirect("guanli.aspx");
    }

    //绑定GridView数据源Oracle数据库
    public void Oraclebind()
    {
        this.oraclecon = new OracleConnection(OracleconnectionString);
        string SelectStr = "SELECT * FROM XDFRYXX WHERE ID = '" + bianhao.Text + "'";
        OracleCommand com = new OracleCommand(SelectStr, oraclecon);
        OracleDataAdapter da = new OracleDataAdapter();
        oraclecon.Open();
        da.SelectCommand = com;
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        com.ExecuteNonQuery();
        oraclecon.Close();
    }

    //GridView编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Oraclebind();
    }

    //GridView取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Oraclebind();
    }

    //GridView更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ID = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim().ToString();
        string NAME = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim().ToString();
        string SEX = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim().ToString();
        string AGE = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim().ToString();
        string BIRTHDAY = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim().ToString();
        string DEPARTMENT = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim().ToString();
        string BEIZHU = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim().ToString();
        string biyeyuanxiao = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim().ToString();
        string biyeshijian = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.Trim().ToString();
        string gongzuojingli = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.Trim().ToString();
        string danrenzhiwei = ((System.Web.UI.WebControls.TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.Trim().ToString();

        if (NAME == "")
        {
            MessageBox.Show("姓名不能为空!");
            return;
        }
        if (SEX == "")
        {
            MessageBox.Show("性别不能为空!");
            return;
        }
        if (DEPARTMENT == "")
        {
            MessageBox.Show("部门不能为空!");
            return;
        }
    }

    //导入
    protected void daoru_Click(object sender, EventArgs e)
    {
        this.sqlcon = new SqlConnection(SqlconnectionString);
        SqlDataAdapter da = new SqlDataAdapter();    
        sqlcon.Open();
        foreach (DataRow dr in ds.Tables[0].Rows) //问题就在这里,提示我无法找到表0,大神帮忙看下        
        {
            string strsql = string.Format(@"select * from xdfryxxb where id = '[0]'", dr["id"].ToString());
            SqlCommand comm = new SqlCommand(strsql, this.sqlcon);
            da.SelectCommand = comm;
            int nCount = ds.Tables[0].Rows.Count;
            if (nCount == 0)
            {
                string InsertStr = string.Format(@"insert into xdfryxxb (id, names, sex, age, birthday, department, beizhu, biyeyuanxiao, biyeshijian, gongzuojingli, danrenzhiwei) 
                                                 values ('[0]','[1]','[2]','[3]','[4]','[5]','[6]','[7]','[8]','[9]','[10]')", dr["id"].ToString(), dr["names"].ToString(), dr["sex"].ToString(),
                                                 dr["age"].ToString(), dr["birthday"].ToString(), dr["department"].ToString(), dr["beizhu"].ToString(), dr["biyeyuanxiao"].ToString(), 
                                                 dr["biyeshijian"].ToString(), dr["gongzuojingli"].ToString(), dr["danrenzhiwei"].ToString());
                SqlCommand commm = new SqlCommand(InsertStr, this.sqlcon);
                commm.ExecuteNonQuery();
                MessageBox.Show("插入成功");
            }
            else
            {
                MessageBox.Show("已存在该条记录,请勿重复插入");
                return;
            }
        }
        sqlcon.Close();
        sqlcon.Dispose();
    }

    //返回
    protected void fanhui_Click(object sender, EventArgs e)
    {
        Response.Redirect("guanli.aspx");
    }

}

本人初学C#,希望大神多多指点
------解决思路----------------------
public void Oraclebind()
    {
        this.oraclecon = new OracleConnection(OracleconnectionString);
        string SelectStr = "SELECT * FROM XDFRYXX WHERE ID = '" + bianhao.Text + "'";
        OracleCommand com = new OracleCommand(SelectStr, oraclecon);
        OracleDataAdapter da = new OracleDataAdapter();
        oraclecon.Open();
        da.SelectCommand = com;
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        com.ExecuteNonQuery(); //不需要
        oraclecon.Close();
    }

“无法找到表0”,调试看下生成的sql,并带到数据库中执行一下就知道问题所在了。
  相关解决方案