读书人

各位大侠给小弟我看看啊 小弟我是新手

发布时间: 2012-06-05 13:54:06 作者: rapoo

各位大侠给我看看啊 我是新手 真心求教!

private void buttonX1_Click(object sender, EventArgs e)
{


textBoxX3.Enabled = true;
textBoxX4.Enabled = true;
textBoxX5.Enabled = true;
textBoxX6.Enabled = true;
textBoxX7.Enabled = true;
textBoxX8.Enabled = true;


dt=mymeans.getDataTable("select * from tb_student where StuId='" + textBoxX1.Text + "'");

if (dt.Rows.Count == 0)
{
MessageBox.Show("您好!您输入的学号不正确或者不存在该学生信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBoxX1.Text = "";
textBoxX1.Focus();
}
else
{
dt = mymeans.getDataTable("select * from tb_student where StuId='" + textBoxX1.Text + "'");

textBoxX2.Text = dt.Rows[0].ItemArray[0].ToString();
textBoxX3.Text = dt.Rows[0].ItemArray[1].ToString();
textBoxX4.Text = dt.Rows[0].ItemArray[2].ToString();
textBoxX8.Text = dt.Rows[0].ItemArray[3].ToString();
textBoxX5.Text = dt.Rows[0].ItemArray[4].ToString();
textBoxX6.Text = dt.Rows[0].ItemArray[5].ToString();
textBoxX7.Text = dt.Rows[0].ItemArray[7].ToString();
textBoxX9.Text = dt.Rows[0].ItemArray[8].ToString();
string sql = ("select Photo from tb_student where StuId='"+textBoxX1.Text+"'");
SqlCommand cmd = new SqlCommand(sql, MyMeans.My_conn);


byte[] b = (byte[])cmd.ExecuteScalar();
if (b.Length > 0)
{
MemoryStream stream = new MemoryStream(b,true);
stream.Write(b,0,b.Length);
pictureBox1.Image = new Bitmap(stream);
stream.Close();
}



mymeans.conn_close();
}


}


问题补充:不加:cmd.Connection.Open(); 错误:ExecuteScalar 要求已打开且可用的 Connection。连接的当前状态为已关闭


加:cmd.Connection.Open(); 错误:ConnectionString 属性尚未初始化。

[解决办法]
连接字符串就是一个字符串嘛,样式类似string strConn = "Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa;Password=";
里边四个参数,你自己根据你的数据库进行设置就行了,比如把Northwind换成master。
另外,这个字符串也可以从webconfig获取。
[解决办法]
一般连接数据库是使用如下步骤

1、先建立connection:
SqlConnection conn = new SqlConnection(connStr);
//这里connStr是连接字符串,根据数据库不同略有不同,一般只要网上搜索一下就能知道。
//注:SQL以外的数据库,用ODBC连接的,一般用OleDbConnection,或者有些数据库有独立的第三方.NET库,但用法都是一样的。
2、打开连接
conn.Open(); //打开数据库连接,这句执行完以后就相当于一个用户登录数据库了,用户一般在connStr里确定
3、建立Command
SqlCommand cmd = conn.CreateCommand();
或SqlCommand = new SqlCommand(CommandText,conn);
ADO.NET中SqlCommand有多种初始化方法,但最终都要和一个已经打开的数据库连接绑定才能发挥作用。
cmd.CommandText = "SELECT * FROM xxx WHERE name=@name"; //这里可以指定SQL语句,SQL的语法根据数据库不同而不同
cmd.Parameter.Add("@name",SqlDbType.NVarChar,50).Value="张三";//为了防SQL注入以及保证数据格式一致,建议用这种方法来增加可变参数。


//同样,OleDb和第三方库也有其对应的版本
4、执行command
cmd.ExcuteNonQuery();
cmd.ExcuteReader();
cmd.ExcuteScalar();
//对应不同的语句,有不同的调用方法,但不论哪种,语句都是在数据库端被执行的。
5、关闭连接回收资源
使用conn.Open();//开启的连接要记得关闭,否则会占用数据库的可用连接数
conn.Close();
conn.Dispose();



请参考:http://msdn.microsoft.com/zh-cn/library/27y4ybxw(v=VS.80).aspx
[解决办法]

C# code
 public class DBHelper    {        //属性:数据库链接对象        private static SqlConnection conn;        public static SqlConnection Conn        {            get            {                try                {                    string connstr = "server=.;database=Driver;User ID=sa;Password=123456";                    if (conn == null)                        conn = new SqlConnection(connstr);                    if (conn.State == ConnectionState.Closed)                        conn.Open();                    if (conn.State == ConnectionState.Broken)                    {                        conn.Close();                        conn.Open();                    }                    return conn;                }                catch (Exception ex)                {                    throw;                }            }        }        //方法:查询,DataReader        public static SqlDataReader GetReader(string SqlStr)        {            SqlCommand cmd = new SqlCommand(SqlStr, Conn);            return cmd.ExecuteReader();        }        public static SqlDataReader GetReader(string SqlStr, SqlParameter[] paras)        {            SqlCommand cmd = new SqlCommand(SqlStr, Conn);            cmd.Parameters.AddRange(paras);            return cmd.ExecuteReader();        }        //查询:DataTable        public static DataTable GetTable(string SqlStr)        {            try            {                  SqlDataAdapter dap = new SqlDataAdapter(SqlStr, Conn);            DataSet ds = new DataSet();            dap.Fill(ds);            conn.Close();            return ds.Tables[0];            }            catch (Exception ex)            {                throw;            }        }        //增删改        public static bool Execute(string SqlStr)        {            SqlCommand cmd = new SqlCommand(SqlStr, Conn);            int result = cmd.ExecuteNonQuery();            conn.Close();            return result > 0;        }        //返回首行首列        public static object GetScalar(string SqlStr)        {            SqlCommand cmd = new SqlCommand(SqlStr, Conn);            object obj = cmd.ExecuteScalar();            conn.Close();            return obj;        } 

读书人网 >C#

热点推荐