还是有错误啊:名称"thisConnection"在类或命名空间XX.Form1中不存在; 名称"zcAdapter"在类或命名空间XX.Form1中不存在 名称"thisDataSet"在类或命名空间XX.Form1中不存在 找不到类型或命名空间名称"thisDataSet"(是否缺少using指令或程序集引用) 找不到类型或命名空间名称"zcAdapter"(是否缺少using指令或程序集引用) 找不到类型或命名空间名称"thisConnection"(是否缺少using指令或程序集引用)这个是我的using指令 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient;
回liujia_0421() 我的初衷就是这个..可是表述不出来.我要代码.麻烦你了
这写的一个,测试是通过的,你根据你的情况改一下吧,如果有问题再问我...private void button1_Click(object sender, EventArgs e) { //查询 SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421"); try { con.Open(); SqlCommand cmd = new SqlCommand("select * from studentDetails where sno=@Sno", con); cmd.Parameters.AddWithValue("@Sno", this.textBox1.Text); SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.Read()) { this.textBox2.Text = sdr["sname"].ToString(); } sdr.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } private void button2_Click(object sender, EventArgs e) { //更新 SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421"); try { con.Open(); SqlCommand cmd = new SqlCommand("update studentDetails set sname=@Sname where sno=@Sno", con); cmd.Parameters.AddWithValue("@Sno", this.textBox1.Text.Trim()); cmd.Parameters.AddWithValue("Sname", this.textBox2.Text.Trim()); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } }
"Database=DataBase");
zcAdapter = new SqlDataAdapter("SELECT * FROM Table", thisConnection);
thisDataSet = new DataSet();
zcAdapter.Fill(thisDataSet, "Table");
foreach (DataRow thisRow in thisDataSet.Tables["name"].Rows)
{
if(thisRow["ID"]==textBox2.text)
messageBox.show(thisRow["ID"].ToString());
}
thisConnection.Close();
{
SqlConnection mySqlConnection =
new SqlConnection("server=7C432EE094B64A0;database=jam;integrated security=SSPI");
SqlCommand mySqlCommand =mySqlConnection.CreateCommand();
mySqlCommand.CommandText=
" SELECT CustomerName " +
" FROM Customers " +
" WHERE CustomerID = '"+textBox1.Text+"'"; mySqlConnection.Open();
SqlDataReader mySqlDataReader=mySqlCommand.ExecuteReader();
if(mySqlDataReader.Read())
{
textBox2.Text = mySqlDataReader["CustomerName"].ToString();
}
mySqlDataReader.Close();
mySqlConnection.Close(); }我的程序是这样.输入ID可以查询出Name.可是不能在textbox中修改数据库.所以想用绑定textBox到表上修改.但是不会操作.请指点
zcAdapter = new SqlDataAdapter(" SELECT CustomerName " +
" FROM Customers " +
" WHERE CustomerID = '"+textBox1.Text+"'",thisConnection);
thisDataSet = new DataSet();
zcAdapter.Fill(thisDataSet, "name");
foreach (DataRow thisRow in thisDataSet.Tables["name"].Rows)
{
if(thisRow["ID"]==textBox2.text) //如果是这个
thisRow["Name"]=textBox1.text;
}
zcAdapter.Updata();
thisConnection.Close();
...
名称"zcAdapter"在类或命名空间XX.Form1中不存在
名称"thisDataSet"在类或命名空间XX.Form1中不存在
找不到类型或命名空间名称"thisDataSet"(是否缺少using指令或程序集引用)
找不到类型或命名空间名称"zcAdapter"(是否缺少using指令或程序集引用)
找不到类型或命名空间名称"thisConnection"(是否缺少using指令或程序集引用)这个是我的using指令
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
{
//查询
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from studentDetails where sno=@Sno", con);
cmd.Parameters.AddWithValue("@Sno", this.textBox1.Text);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
this.textBox2.Text = sdr["sname"].ToString();
}
sdr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
} private void button2_Click(object sender, EventArgs e)
{
//更新
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
try
{
con.Open();
SqlCommand cmd = new SqlCommand("update studentDetails set sname=@Sname where sno=@Sno", con);
cmd.Parameters.AddWithValue("@Sno", this.textBox1.Text.Trim());
cmd.Parameters.AddWithValue("Sname", this.textBox2.Text.Trim());
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
[email protected]
private SqlDataAdapter zcAdapter;
private DataSet thisDataSet;
加上这些声明.