博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ADO.Net

Posted on 2009-03-21 16:47  Aimee  阅读(119)  评论(0)    收藏  举报
进行配置:和数据库建立连接
视图--〉服务器配置管理器--〉数据库连接--〉添加连接。。
基本配置:
注册驱动得到Connection
需要Command对象
打开数据库
操作数据
关闭数据库
ADMIN-330A87569

1 非类型化的数据
DataSet---tables

SqlConnection sqlconn = new SqlConnection(@"Data Source=ADMIN-330A87569;Initial Catalog=FamilyBillme;User ID=sa");
            SqlCommand sqlcomm = new SqlCommand("select Account.* from Account", sqlconn);

            sqlconn.Open();
  //显示的打开数据库
            SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
            DataSet ds = new DataSet();
            sda.Fill(ds);

            sqlconn.Close();

加载SQL语句
 private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlconn = new SqlConnection(@"Data Source=ADMIN-330A87569;Initial Catalog=FamilyBillme;User

ID=sa;PassWord=wxl6419501");
            SqlCommand sqlcomm = new SqlCommand("select Account.* from Account", sqlconn);

            //sqlconn.Open();
            DataSet ds;
            using (SqlDataAdapter sda = new SqlDataAdapter(sqlcomm))
            {
                 ds = new DataSet();
                sda.Fill(ds);
            }
            //sqlconn.Close();
            this.dataGridView1.DataSource = ds.Tables[0];

        }
-----------------------------------------------------------------------
加载Proc
 SqlConnection sqlconn = new SqlConnection(@"Data Source=ADMIN-330A87569;Initial Catalog=FamilyBillme;User

ID=sa;PassWord=wxl6419501");
            SqlCommand sqlcomm = new SqlCommand("usp_Account_LoadAccountData", sqlconn);
        
            //sqlconn.Open();
            sqlcomm.CommandType = CommandType.StoredProcedure;
            DataSet ds;
            using (SqlDataAdapter sda = new SqlDataAdapter(sqlcomm))
            {
               ds = new DataSet();
                sda.Fill(ds);
            }
            //sqlconn.Close();
            this.dataGridView1.DataSource = ds.Tables[0];
数据逻辑层的命名规则:直接使用数据库中的表名
2 类型化的数据

1 guid操作
2 存储过程中返回值

 

if (param != null)
            {
                sqlcomm.Parameters.Add(param);
            }


sqlcomm.ExecuteNonQuery();执行非查询语句

 

用asp做的,图片或者flash链接,用javascript:

用一个简单的 <img src="...."nbsp; onclick="link()"/gt;
<script language = "javascript"gt;
function link()
{
   window.location.href = "要跳转的页面";
}
</script>

tab:
shift+tab : 回退一格

*********************************************


1,

SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=;database=Demo");

SqlCommand cmd = new SqlCommand("你的存储过程名", cn);

cmd.CommandType = CommandType.StoredProcedure;

//下面添加的输出参数名字和存储过程应该一样

cmd.Parameters.Add("@outputParam", SqlDbType.Int);

cmd.Parameters["@outputParam"].Direction = ParameterDirection.Output;

cn.Open();

cmd.ExecuteNonQuery();

cn.Close();

object o =  cmd.Parameters["@outputParam"].Value;

2,

类似于如下的样子:

SqlParameter para = new SqlParameter("@paraName", SqlDbType.Int);
para.Direction = ParameterDirection.Output;

command.Exe...
object objValue = para.Value;

3,

C#获取存储过程的Return返回值和Output输出参数值
作者:dnawo 日期:2008-04-10
字体大小: 小 中 大 
1.获取Return返回值


程序代码
//存储过程
//Create PROCEDURE MYSQL
//    @a int,
//    @b int
//AS
//    return @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 10;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
MyCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@return"].Value.ToString());

2.获取Output输出参数值


程序代码
//存储过程
//Create PROCEDURE MYSQL
//    @a int,
//    @b int,
//    @c int output
//AS
//    Set @c = @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@c", SqlDbType.Int));
MyCommand.Parameters["@c"].Direction = ParameterDirection.Output;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@c"].Value.ToString());

 

--------学习体会

1,创建带有返回值的存储过程

select * from cdlist

drop proc pro_int

create procedure pro_int

as

    return 6;

    return 4;--注意是对的

go

 

declare @i int

exec @i=pro_int

print @i           (执行将打印6)

2。