public void InsertText(string SQLstr, string test, string indexName)
{
scmd = new SqlCommand(SQLstr, myconnection);
myconnection.Open();
SqlDataReader sr = scmd.ExecuteReader();
while (sr.Read())
{
test=sr[indexName].ToString();
}
myconnection.Close();
}
SQLstr = "select * from 会员图书表 where 图书号='" + dataGridView1.SelectedRows[0].Cells["图书号"].Value.ToString() + "'";
string s1 = "";
db.InsertText(SQLstr,s1,"会员号");
textBox5.Text = s1; 我是用
textBox5.Text = s1;
进行测试的,为什么显示不出来呢?谁知道的帮我下下,谢谢`~~~~
解决方案 »
- c# 线程处理问题
- 两个窗体之间控件值的显示问题
- bin、obj、Properties文件夹里面的是什么东西呀?
- [100分求助]:c#.net下直接调用dll没有问题,但是把dll封装在web service 再用c#.net 调用出现问题
- 关于datatable中的rowchanged事件
- Remoting:使用Remoting时,同一个程序可否既是Client又是Server?——详见内。
- 求一句SQL
- datagrid控件同一列数据中固定行和下面内容行的Alignment属性是否可以不一样
- 《向ALL请教》请教个关于 NotifyIcon 的问题
- 请高手帮我看看
- 如何对其它软件的文本框之类的东西进行自动填写
- 菜鸟问题:winform怎么连到Web系统
public void InsertText(string SQLstr, TextBox textbox, string indexName)
{
scmd = new SqlCommand(SQLstr, myconnection);
myconnection.Open();
SqlDataReader sr = scmd.ExecuteReader();
while (sr.Read())
{
textbox=sr[indexName].ToString();
}
myconnection.Close();
}db.InsertText(SQLstr,Textbox5,"会员号");如果是这样改的话,textbox5就可以显示,只是我不知道我上面哪里错了
{
scmd = new SqlCommand(SQLstr, myconnection);
myconnection.Open();
SqlDataReader sr = scmd.ExecuteReader();
while (sr.Read())
{
test=sr[indexName].ToString();
}
myconnection.Close();
}
SQLstr = "select * from 会员图书表 where 图书号='" + dataGridView1.SelectedRows[0].Cells["图书号"].Value.ToString() + "'";
string s1 = "";
db.InsertText(SQLstr, ref s1,"会员号");
textBox5.Text = s1;
若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。
如果想在方法中返回,则应加上out or ref关键字
public void InsertText(string SQLstr, (out|ref) string test, string indexName)
{
scmd = new SqlCommand(SQLstr, myconnection);
myconnection.Open();
SqlDataReader sr = scmd.ExecuteReader();
while (sr.Read())
{
test=sr[indexName].ToString();
}
myconnection.Close();
}调用时 db.InsertText(sqllstr, out sl, ....)
db.InsertText(SQLstr,s1,"会员号");
textBox5.Text = s1;
直接操作的话s1的值始终为"",直到其释放掉
而如楼上在InsertText中+ref s1的话,方法不仅会return;还会return s1。
这样s1就改变了
string属于值类型,当作普通参数传入函数时,是不会在函数外反应出string值的变化的。
对于值类型参数,使用ref关键字,那么传递给方法的将是这个参数的地址,而不是参数的值类型实例的拷贝。
假如一个方法的参数加上ref关键字,那么在调用该方法之前必须首先初始化该参数,被调用方法可以任意选择读取或修改该参数的值。
对于引用类型参数,传递给方法的就是这个参数的地址,所以一般不需要使用ref关键字(除了用new对这个参数重新赋值)。
实际上不明白的就是new一个引用类型的实例,它返回指向该引用类型实例在托管堆的地址的指针,那么在不使用ref关键字的情况下为什么不能直接改变这个引用类型参数呢,难道这个引用类型参数(它本身保存指向托管堆中的内存地址)在堆栈中也有一个自己的地址吗?