如果我没猜错的话你的SQL语句就是第二张载图里面的东东。也就是说,实际的SQL语句里面的where条件等于 "textBox1.get_text()" 这一个字符串我们应该这样给SQL语句赋值 string sql = string.Format("select * from table1 where id = '{0}'",textBox2.Text);
自己写个DateAdapter去Fill public SqlConnection con=new SqlConnection(""); public SqlDataAdapter sda=new SqlDataAdapter("select * from xxx where xxx='"+xxx+"'",con); sda.Fill(ds,tablename);
@学号=TextBox1.Text ...... Where 学号=@学号
@学号=TextBox1.Text ...... Where 学号=@学号 ..............我直接写成:Where 学号=TextBox1.Text 不是更方便.....这样行不通哦,郁闷ing
噢.你是要J#的噢..弱类型的DataSet Fill代码,供你参考SqlConnection cn = new SqlConnection(); DataSet CustomersDataSet = new DataSet(); SqlDataAdapter da; SqlCommand DAUpdateCmd;
//Set the connection string of the SqlConnection object to connect to the SQL Server database in which you created the sample table. cn.set_ConnectionString("Server=server;Database=Northwind;UID=login;PWD=password;"); cn.Open();//To initialize the SqlDataAdapter object, specify a Select command that //retrieves data from the sample table. da = new SqlDataAdapter("select * from CustTest order by CustId", cn);//Initialize the SqlCommand object that will be used as the UpdateCommand for the DataAdapter. //Note that the WHERE clause uses only the CustId field to locate the record to be updated. DAUpdateCmd = new SqlCommand("Update CustTest set CustName = @pCustName where CustId = @pCustId", da.get_SelectCommand().get_Connection());//Create the parameter collection. SqlParameterCollection ParamColl = DAUpdateCmd.get_Parameters();
//Add the parameters to the collection. ParamColl.Add("@pCustName", SqlDbType.VarChar);DAUpdateCmd.get_Parameters().get_Item("@pCustName").set_SourceVersion(DataRowVersion.Current); DAUpdateCmd.get_Parameters().get_Item("@pCustName").set_SourceColumn((System.String)"CustName");ParamColl.Add("@pCustId", SqlDbType.Int);DAUpdateCmd.get_Parameters().get_Item("@pCustId").set_SourceVersion(DataRowVersion.Current); DAUpdateCmd.get_Parameters().get_Item("@pCustId").set_SourceColumn((System.String)"CustId");//Assign the SqlCommand to the UpdateCommand property of the SqlDataAdapter. da.set_UpdateCommand(DAUpdateCmd); da.Fill(CustomersDataSet, "Customers"); System.Console.WriteLine("Customer Name before Update : " + CustomersDataSet.get_Tables().get_Item("Customers").get_Rows().get_Item(0).get_Item("CustName"));CustomersDataSet.get_Tables().get_Item("Customers").get_Rows().get_Item(0).set_Item("CustName","Jack"); da.Update(CustomersDataSet, "Customers"); System.Console.WriteLine("Customer Name updated successfully");cn.Close(); Console.ReadLine();
我的数据库里学号那里有"11","1 1"," 11","11 "," 11 "," 1 1","1 1 "," 1 1 "(我怕"+textBox1.get_Text()+"里会有空格,我就在数据库的学号那栏里添加了带空格的学号,用WHERE (学号='11') 或者 WHERE (学号='1 1')、WHERE (学号=' 11')、WHERE (学号='11 ')、WHERE (学号=' 11 ')等都能够找到相应的数据,可是用textBox1.get_Text的方法返回的就是NULL怎么办哦那位大哥大姐牛人告诉我该怎么改,才能让它找学号为textBox1里的学生.....
另外,我的学号也是字符串类型的,所以Convert.toInt32不行.....而且它都不提示类型不匹配,直接返回NULL
你要在文本框里输入用户名和密码吧?
当你输完后要按回车登陆吧?
这时候它就要检查你的用户名在数据库里是否存在吧?
如果不存在就要报错吧?
那这时候不就要把textBox里的那个用户名传给数据库?
改成WHERE (学号='"+textBox1.get_Text()+"')就不行了,我估计这里把"+textBox1.get_Text()+"整个当成一个字符串来判断了,也就是它根本没有去读取Form1.textBox1里的内容~~
textbox1.get_text() 这个两边是不能加双引号的,比如本来textbox1.get_text()的值是12,然后你加了引号后就成了WHERE (学号='"+textBox1.get_Text()+"')然后查询的是学号是“textBox1.get_Text()”,而不是12的再看看是不是这个样子?
登陆的时候要看输入的学号是否存在于数据库里,如果没有,那么MsgBox("用户不存在")
那么问题就来啦,怎么样把Form1上的textBox1里的学号传给数据库呢?
光用WHERE (学号='11')不能解决问题啊!
WHERE (学号='"+textBox1.get_Text()+"')才是解决的办法,可是这样写又不对.....郁闷ing..........
string sql = string.Format("select * from table1 where id = '{0}'",textBox2.Text);
这个不是强转换成32位整数类型吗?
有没有试过
Where= (学号='"+textBox1.get_Text()+"')或者是你语法错误了,J#我不是很了解
你的执行语句应该是
FillBy1(Table, string)另:LZ你咋想到用J#的
private void fillBy1ToolStripButton_Click(Object sender, System.EventArgs e)
{
try
{
this._3606DataSetstudentTableAdapter.FillBy1(this._3606DataSet.get_student());
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.get_Message());
} }
用J#很方便,代码几乎全是它自动添加,整个运行速度快,界面整洁,总之Microsoft的东西用起来就是一个字"爽"
Netbeans..太慢......Eclipse..麻烦.....
说解决方法:
1.按照17楼的,在向导里把你的where 改成 学号 = @学号
2.把你的语句改成
this._3606DataSetstudentTableAdapter.FillBy1(this._3606DataSet.get_student(), textBox1.get_Text());
//我不会J#,所以请自行保证textBox1.get_Text()是正确取得textBox1的Text的
<DbSource ConnectionRef="_3606CSTR (Settings)" DbObjectName="student" DbObjectType="Table" FillMethodModifier="Public" FillMethodName="FillBy1" GenerateMethods="Both" GenerateShortCommands="True" GeneratorGetMethodName="GetDataBy1" GeneratorSourceName="FillBy1" GetMethodModifier="Public" GetMethodName="GetDataBy1" QueryType="Rowset" ScalarCallRetval="System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="True" UserGetMethodName="GetDataBy1" UserSourceName="FillBy1">
<SelectCommand>
<DbCommand CommandType="Text" ModifiedByUser="True">
<CommandText>SELECT ID, 姓名, 性别, 学号, 密码, 联系方式, 身份证号, 出生日期
FROM student
WHERE (学号 = '11')</CommandText>
<Parameters>
</Parameters>
</DbCommand>
</SelectCommand>
</DbSource>
2.把你的语句改成
this._3606DataSetstudentTableAdapter.FillBy1(this._3606DataSet.get_student(), textBox1.get_Text());
1错误 无法分析@附近的文本2错误 1 在“Student._3606DataSetstudentTableAdapter”中找不到方法“FillBy1(studentDataTable, String)” C:\Program Files\Microsoft Visual Studio 8\Projects\Student\Student\Form2.jsl 470 4 Student
public SqlDataAdapter sda=new SqlDataAdapter("select * from xxx where xxx='"+xxx+"'",con);
sda.Fill(ds,tablename);
......
Where 学号=@学号
......
Where 学号=@学号
..............我直接写成:Where 学号=TextBox1.Text 不是更方便.....这样行不通哦,郁闷ing
第1步做完后,你帖子里发的第1张图片那里,应该显示FillBy1, GetDataBy1 (@学号) 了吧?
如果是的话应该没什么问题了,你再看看FillBy1的参数是什么,可能学号是个int?
我数据库里的学号是String类型的...
DataSet CustomersDataSet = new DataSet();
SqlDataAdapter da;
SqlCommand DAUpdateCmd;
//Set the connection string of the SqlConnection object to connect to the SQL Server database in which you created the sample table.
cn.set_ConnectionString("Server=server;Database=Northwind;UID=login;PWD=password;");
cn.Open();//To initialize the SqlDataAdapter object, specify a Select command that
//retrieves data from the sample table.
da = new SqlDataAdapter("select * from CustTest order by CustId", cn);//Initialize the SqlCommand object that will be used as the UpdateCommand for the DataAdapter.
//Note that the WHERE clause uses only the CustId field to locate the record to be updated.
DAUpdateCmd = new SqlCommand("Update CustTest set CustName = @pCustName where CustId = @pCustId", da.get_SelectCommand().get_Connection());//Create the parameter collection.
SqlParameterCollection ParamColl = DAUpdateCmd.get_Parameters();
//Add the parameters to the collection.
ParamColl.Add("@pCustName", SqlDbType.VarChar);DAUpdateCmd.get_Parameters().get_Item("@pCustName").set_SourceVersion(DataRowVersion.Current);
DAUpdateCmd.get_Parameters().get_Item("@pCustName").set_SourceColumn((System.String)"CustName");ParamColl.Add("@pCustId", SqlDbType.Int);DAUpdateCmd.get_Parameters().get_Item("@pCustId").set_SourceVersion(DataRowVersion.Current);
DAUpdateCmd.get_Parameters().get_Item("@pCustId").set_SourceColumn((System.String)"CustId");//Assign the SqlCommand to the UpdateCommand property of the SqlDataAdapter.
da.set_UpdateCommand(DAUpdateCmd);
da.Fill(CustomersDataSet, "Customers"); System.Console.WriteLine("Customer Name before Update : " + CustomersDataSet.get_Tables().get_Item("Customers").get_Rows().get_Item(0).get_Item("CustName"));CustomersDataSet.get_Tables().get_Item("Customers").get_Rows().get_Item(0).set_Item("CustName","Jack");
da.Update(CustomersDataSet, "Customers"); System.Console.WriteLine("Customer Name updated successfully");cn.Close();
Console.ReadLine();
因爲你的TableAdapter是用向導生成的,不能在裏面寫TextBox1.Text 之類的東西,要設置成變量@学号。
然後在外面寫
Xuehao=TextBox1.Text
studentTableAdapter.FillBy1(DataSet.student,Xuehao)//DataSet設置成你DataSet名稱
DataGridView1.DataSource=DataSet.student //加入你要顯示在DataGridView1裏面而且這樣寫更規範,不能寫死為TextBox1.Text 之類的東西,假如沒有TextBox1呢?是ComboBox呢?
它说"@"附近的WHERE子句错误,无法分析查询文本...
其实向导里不用加"@"直接写成下面的格式
SELECT ID, 姓名, 性别, 学号, 密码, 联系方式, 身份证号, 出生日期
FROM student
WHERE (学号 = stnum)点"完成",向导配置完毕!到这里还没完成,然后要在
FillBy的Parameters里添加一个Parameter,并把它的ParameterName设定为stnum,大功告成!
给大家看看我的截图:
你说:
"在Form1里测试的时候,textBox1.get_Text()是有值的,但是在那个TableAdapter配置向导里我就不知道它有没有值了....."那就是你取值成功,而赋值出了问题.
7楼说的"数据层应该与ui分离",本人也认同.
也就是说tableUpdate查询配置向导 这个东西不能识别你的TextBox1.get_Test(),它没有using system.text这些类库,你试下把From1下的using ....后面全部命名空间去掉,你的代码看看能不能获取textBox1.text的值?解决方法:
你可以通过传参方式 @参数,把textBox.text的值传过去.
如"select * from student where(学号='@参数名')" (取textBox1.text值赋给参数,再传参)愚蠢的见解,请楼主选择参考.
在aspx的html代码里能看到,