aspnet中的存儲過程
1. 在asp.net(c#)中如何用sql語句調用伺服器端的存儲過程
如果已知了
伺服器端
有你需要的
存儲過程
,則第一步需要使你的程序和伺服器先建立起連接,連接成功之後可以寫一個
sql語句
:exec
存儲過程名(不帶參的存儲過程)就行了,當然這些可以在你的程序里寫也可以在你的程序里寫語句用來調用完成上述sql語句的存儲過程名即把上面的exec
存儲過程名
封裝在另一個存儲過程里,在程序里通過.net
里的
sqlcommand
和
sqlconnection
類來實現完成上面所述的語句
2. C#.net中如何使用存儲過程
資料庫中
create proc User_Login
(
@LoginCode nvarchar(20),
@password nvarchar(50)
)
as
select count(*) from [你的用戶表名] where LoginCode =@LoginCode ,password=@password
public bool User_login(Users user)//用戶登錄
{
SqlCommand cmd = new SqlCommand("User_Login", conn); //User_Login是你資料庫里的存儲過程名稱。
cmd.CommandType = CommandType.StoredProcere;
cmd.Parameters.Add("@LoginCode", SqlDbType.VarChar, 50).Value = user.logincode;//為存儲過程的用戶名賦值。
cmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = user.pwd;//為存儲過程的密碼賦值。
try
{
conn.Open();
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
{
return true;
}
else
return false;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
conn.Close();
cmd.Dispose();
}
}
3. 如何在網站建設(asp.net)中使用存儲過程
我有項目用的是存儲過程 你要就告訴我 public static SqlCommand CreateCmd(string procName, SqlParameter[] prams, SqlConnection Conn)
{
SqlConnection SqlConn = Conn;
if (SqlConn.State.Equals(ConnectionState.Closed))
{
SqlConn.Open();
}
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.StoredProcere;//按照存儲過程解決
Cmd.Connection = SqlConn;
Cmd.CommandText = procName;//存儲過程名
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
if (parameter != null)
{
Cmd.Parameters.Add(parameter);
}
}
}
return Cmd;
}
4. 資料庫中有幾萬條數據,怎樣使用aspnetpage分頁控制項結合資料庫分頁讀取呢
可以用存儲過程分頁
/*
函數名稱: GetRecordFromPage
函數功能: 獲取指定頁的數據
參數說明: @tblName 包含數據的表名
@fldName 關鍵欄位名
@PageSize 每頁記錄數
@PageIndex 要獲取的頁碼
@OrderType 排序類型, 0 - 升序, 1 - 降序
@strWhere 查詢條件 (注意: 不要加 where)
*/
CREATE PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 欄位名
@PageSize int = 10, -- 頁尺寸
@PageIndex int = 1, -- 頁碼
@OrderType bit = 0, -- 設置排序類型, 非 0 值則降序
@strWhere varchar(2000) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主語句
declare @strTmp varchar(1000) -- 臨時變數
declare @strOrder varchar(500) -- 排序類型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName + '] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp = ''
if @strWhere != ''
set @strTmp = ' where (' + @strWhere + ')'
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
exec (@strSQL)
GO
5. 代碼中使用aspnet_User表需要用到什麼引用
1.先做成實體類。即model層。欄位封裝好之後。在DAL裡面導入命名空間就可以用。
2.如果你問的不是這個問題,那麼應該指的是VS直接連接資料庫,然後代碼裡面引用表結構。
這個就需要用到LINQ了。VS2008集成的有LINQ。
如果你是VS2008,那麼就很簡單了。
首先,用VS連接資料庫,(伺服器資源管理器--右鍵資料庫連接---添加連接--MicrosoftSqlServer--後面的呢自己輸了。。。)
連接好之後。
1.右鍵你解決方案下面的網站,然後選擇「添加新項」,在打開的對話框中找到「LINQtoSQL類」添加進來。
2.他會提示你添加到APP_Code文件夾下面,這些都不用管。你只管到左邊你已經添加好的資料庫中找到你想要添加的表。
3.打開你添加的「LINQ」名字你一般不要修改,叫「DataClasses.dbml」,雙擊打開,把選好的表拖進去。這樣引用表就完成了,下面是讓他自動生成方法。
4.把你想要對表的操作寫成存儲過程,然後把存儲過程直接拖到右側,我下面給你的例子左邊有3張拖進去的表,右邊有3個拖進去的存儲過程。拖好之後,記住保存。
5.最後,你會發現「DataClasses.dbml」下面有一個類:「DataClasses.designer.cs」這個裡面就是他根據你的表還有存儲過程生成的代碼,類似DAL層,但是他裡面還有你的表的實體,欄位的封裝,方法統統都有,屬於是3層的融合,它裡面有自己的一些獨特屬性,你可以研究研究。
6.等所有操作完成之後,你在類裡面引入命名空間,然後就調用方法就可以了。。真的很方便,下面我給你貼一張圖,你看看。然後底下是自動生成的代碼其中的一小段,因為我用了3長表,代碼太多,只貼一段代碼給你看。
#pragmawarningdisable1591
//------------------------------------------------------------------------------
//<auto-generated>
//此代碼由工具生成。
//運行庫版本:2.0.50727.3603
//
//對此文件的更改可能會導致不正確的行為,並且如果
//重新生成代碼,這些更改將會丟失。
//</auto-generated>
//------------------------------------------------------------------------------
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Data.Linq;
usingSystem.Data.Linq.Mapping;
usingSystem.Linq;
usingSystem.Linq.Expressions;
usingSystem.Reflection;
[System.Data.Linq.Mapping.DatabaseAttribute(Name="PropertyHome")]
:System.Data.Linq.DataContext
{
privatestaticSystem.Data.Linq.Mapping.MappingSourcemappingSource=newAttributeMappingSource();
#
partialvoidOnCreated();
partialvoidInsertT_PH_UserEstinfo(T_PH_UserEstinfoinstance);
partialvoidUpdateT_PH_UserEstinfo(T_PH_UserEstinfoinstance);
partialvoidDeleteT_PH_UserEstinfo(T_PH_UserEstinfoinstance);
#endregion
publicDataClassesDataContext():
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["PropertyHomeConnectionString"].ConnectionString,mappingSource)
{
OnCreated();
}
publicDataClassesDataContext(stringconnection):
base(connection,mappingSource)
{
OnCreated();
}
publicDataClassesDataContext(System.Data.IDbConnectionconnection):
base(connection,mappingSource)
{
OnCreated();
}
publicDataClassesDataContext(stringconnection,System.Data.Linq.Mapping.MappingSourcemappingSource):
base(connection,mappingSource)
{
OnCreated();
}
publicDataClassesDataContext(System.Data.IDbConnectionconnection,System.Data.Linq.Mapping.MappingSourcemappingSource):
base(connection,mappingSource)
{
OnCreated();
}
publicSystem.Data.Linq.Table<agent_score>agent_score
{
get
{
returnthis.GetTable<agent_score>();
}
}
publicSystem.Data.Linq.Table<estinfo>estinfo
{
get
{
returnthis.GetTable<estinfo>();
}
}
publicSystem.Data.Linq.Table<T_PH_UserEstinfo>T_PH_UserEstinfo
{
get
{
returnthis.GetTable<T_PH_UserEstinfo>();
}
}
[Function(Name="dbo.PA_GetEstByCode")]
publicISingleResult<PA_GetEstByCodeResult>PA_GetEstByCode([Parameter(DbType="VarChar(50)")]stringestCode,[Parameter(Name="Type",DbType="Int")]System.Nullable<int>type)
{
IExecuteResultresult=this.ExecuteMethodCall(this,((MethodInfo)(MethodInfo.GetCurrentMethod())),estCode,type);
return((ISingleResult<PA_GetEstByCodeResult>)(result.ReturnValue));
}
[Function(Name="dbo.PA_GetPlate")]
publicISingleResult<PA_GetPlateResult>PA_GetPlate([Parameter(DbType="Int")]System.Nullable<int>op_no,[Parameter(DbType="Int")]System.Nullable<int>page,[Parameter(DbType="Int")]System.Nullable<int>rows,[Parameter(DbType="VarChar(50)")]stringuserid,[Parameter(DbType="VarChar(50)")]stringestName)
{
IExecuteResultresult=this.ExecuteMethodCall(this,((MethodInfo)(MethodInfo.GetCurrentMethod())),op_no,page,rows,userid,estName);
return((ISingleResult<PA_GetPlateResult>)(result.ReturnValue));
}
[Function(Name="dbo.PA_GetEstInfoByName")]
publicISingleResult<PA_GetEstInfoByNameResult>PA_GetEstInfoByName([Parameter(DbType="VarChar(50)")]stringestName,[Parameter(DbType="VarChar(50)")]stringstfID,[Parameter(DbType="Int")]System.Nullable<int>page,[Parameter(DbType="Int")]System.Nullable<int>rows,[Parameter(Name="Type",DbType="Int")]System.Nullable<int>type)
{
IExecuteResultresult=this.ExecuteMethodCall(this,((MethodInfo)(MethodInfo.GetCurrentMethod())),estName,stfID,page,rows,type);
return((ISingleResult<PA_GetEstInfoByNameResult>)(result.ReturnValue));
}
}
[Table(Name="dbo.agent_score")]
publicpartialclassagent_score
{
privatestring_AgentNo;
privatestring_AgentCName;
privatestring_AgentName;
privatestring_AgentMobile;
privatestring_AgentEmail;
privatestring_AgentLicense;
privatestring_BranchCName;
privateSystem.Nullable<int>_post_counter;
privatestring_agentphoto;
privatestring_Agentpage;
privatestring_enquirypage;
privatestring_scpid;
privatestring_scpMkt;
privatestring_scp_c;
privateSystem.Nullable<double>_agentscore;
privateSystem.Nullable<double>_agentscore1;
privateSystem.Nullable<double>_agentscore2;
privateSystem.Nullable<double>_agentscore3;
privateSystem.Nullable<double>_agentscore4;
privateSystem.Nullable<double>_agentscore5;
privateSystem.Nullable<double>_agentscore6;
publicagent_score()
{
}
[Column(Storage="_AgentNo",DbType="NVarChar(10)NOTNULL",CanBeNull=false)]
publicstringAgentNo
{
get
{
returnthis._AgentNo;
}
set
{
if((this._AgentNo!=value))
{
this._AgentNo=value;
}
}
}
[Column(Storage="_AgentCName",DbType="NVarChar(50)")]
publicstringAgentCName
{
get
{
returnthis._AgentCName;
}
set
{
if((this._AgentCName!=value))
{
this._AgentCName=value;
}
}
}
。。。其它的操作看你自己咯~~~
謝謝,望採納,我很辛苦啊,,,呵呵。。
6. C# .net 是怎麼使用 存儲過程的,來幾句簡單明了的代碼
1.構造與Sql伺服器連接對象
SqlConnection MyCon = new SqlConnection()
2.構造SqlCommand對象
SqlCommand MyCmd = new SqlCommand()
3.指定SqlCommand對象調用的存儲過程
MyCmd.CommandText = "ProcereName"
ProcereName是Sql資料庫中實際存在的存儲過程名
4.聲明SqlCommand對象的命令類型為存儲過程
MyCmd.CommandType = CommandType.StoredProcere
5.構造參數對象
SqlParameter MyParam = new SqlParameter("@ParamName",類型,寬度);
@ParamName:是調用存儲過程的參數,參數名必需和現有的存儲過程一致
類型:SqlDbType.Char字元型,C#中用SqlDbType存儲了Sql數據類型
寬度:類型字寬.如char(15)
6.賦值給參數對象
MyParam.Value = Value
7.將參數添加到SqlCommand對象
MyCmd.Parameters.Add(MyParam)
8.打開Sql連接對象
MyCon.Open()
9.執行SqlCommand
MyCmd.ExecuteNonQuery()
10.關閉Sql連接對象
MyCon.Close()
7. mssql2005 資料庫多出很多 aspnet_xxxx 的數據表
可能的情況:
1.你安裝了什麼網路應用方面的軟體,在你的資料庫中創建的對象
2.能訪問你資料庫的人建的,問問身邊的人是不是做什麼試驗了
3.你的機器被入侵,已經成了肉機了
8. ASPNET怎樣調用一個存儲過程(帶參數的)
ASPNET怎樣調用一個存儲過程(帶參數的)使用的是Ado.NET中的相關的資料庫操作方法。
Ado.NET連接資料庫有以下幾個步驟:
1:使用配置的資料庫連接串,創建資料庫連接 Connection 對象
2:構建操作的sql語句
3:定義command對象
4:打開數據連接
5:執行命令
舉一個例子,刪除操作
public int DeleteStudent3(int stuID)
{
int result = 0;
using (SqlConnection connection = new SqlConnection(connString))
{
SqlCommand objCommand = new SqlCommand(dboOwner + ".usp_DeleteStudent", connection);
objCommand.CommandType = CommandType.StoredProcere;
objCommand.Parameters.Add("@stuID", SqlDbType.Int).Value = stuID;
connection.Open();
result = objCommand.ExecuteNonQuery();
}
return result;
}
9. asp.net(c#)將圖片存入資料庫存儲過程代碼.
其實
只有一條添加的SQL語句,不需要存儲過程,
insert
into
ImageStore
values(,,,,,)首先將
圖片轉換成二進制數據private
Byte[]
setPhotoByte(string
photoPath)
{
if
(photoPath
!=
null
)
{
string
str
=
photoPath;//保存傳入的路徑
//將傳入路徑下的圖片以文件方式打開
file
=
new
FileStream(str,
FileMode.Open,
FileAccess.Read);//以只讀方式打開傳入路勁下的圖片
Byte[]
byteSQLData
=
new
byte[file.Length];
file.Read(byteSQLData,
0,
byteSQLData.Length);//將文件中的數據讀入到創建的byte數據中
file.Close();
MessageBox.Show(photoPath);
return
byteSQLData;
}
}需要傳一個參數
,文件路徑,可以用
OpenFileDialog
選擇圖片最後將轉換好的二進制數據
byteSQLData
直接保存到資料庫
表的欄位ImageData
中完成保存顯示圖片:
將資料庫中數據圖片控制項
.Image
=Image
.FromStream
(new
MemoryStream
(Byte[]
資料庫數據));
即可顯示該圖片
10. asp.net(c#)中如何使用存儲過程請大家幫忙
public DataSet getDataByPage(int pageIndex)
{
DataSet ds = new DataSet();
String m_connectionString = ConfigurationSettings.AppSettings["connectionString"].Trim();
OleDbConnection conn = null;
String tablename = " mytable"; //表名
String orderName = "orderTime"; //排序的欄位名
try
{
conn = new OleDbConnection(m_connectionString);
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "pagination";
cmd.CommandType = CommandType.StoredProcere;
cmd.Connection = conn;
cmd.Parameters.Add(new OleDbParameter("@tablename",OleDbType.VarChar));
cmd.Parameters["@tablename"].Value = tablename;
cmd.Parameters.Add(new OleDbParameter("@tableField",OleDbType.VarChar));
cmd.Parameters["@tableField"].Value = " * " ;
cmd.Parameters.Add(new OleDbParameter("@orderName",OleDbType.VarChar));
cmd.Parameters["@orderName"].Value = orderName;
cmd.Parameters.Add(new OleDbParameter("@pageSize",OleDbType.Integer));
cmd.Parameters["@pageSize"].Value = m_pageSize;
cmd.Parameters.Add(new OleDbParameter("@pageIndex",OleDbType.Integer));
cmd.Parameters["@pageIndex"].Value = pageIndex;
cmd.Parameters.Add(new OleDbParameter("@doCount",OleDbType.Integer));
cmd.Parameters["@doCount"].Value = 0;
cmd.Parameters.Add(new OleDbParameter("@orderType",OleDbType.Integer));
cmd.Parameters["@orderType"].Value = 1;
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds,"music");
}
catch(Exception e)
{
System.Diagnostics.Trace.WriteLine(e.Message);
}
finally
{
conn.Close();
}
return ds;
}
//綁定數據到DateGrid
private void BindData(int pageIndex)
{
DataView dataview = new DataView(this.getDataByPage(pageIndex).Tables["newdata"]);
DataGrid1.DataSource = dataview;
DataGrid1.DataBind();
}
應該沒有問題!