C#连接sqlserver数据库
当在C#语言中,连接sql数据库的时候,常用到sqlconnection建立与数据库的连接。 语法如下:
String sqlcon = "***";//这里是一个字符串,具体跟数据库的验证方式有关。
//sql server 身份验证 连接字符串
private string ConnstrSqlServer = "server=服务器名称;uid=登录名称;pwd=登录密码;database=数据库名称";
//windows 身份验证连接字符串
private string ConnstrWindows = "server=服务器名称;database=数据库名称;Trusted_Connection=SSPI";
SqlConnection connection = new SqlConnection(sqlcon);
例如:
SqlConnection connection = new SqlConnection("server=(local);database=dotNET_lab;Trusted_Connection=SSPI");
//windows验证方式 。
SqlConnection connection = new SqlConnection("server=(local);uid=sa;password=sa;database=dotNET_lab");
//混合验证方式 。
然后,
try {
connection.Open();
SqlCommand command = new SqlCommand("select count(*) from admin where username='"+username+"' and password='"+password+"'",connection);
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
n = reader.GetInt32(0);//n是在之前定义的,如果n>0,显然就存在这样一个用户。
}
//Console.WriteLine(reader.GetString(1));
}
catch(SqlException ex) {
&n
相关文档:
xmlpost by HttpWebRequest:
protected string PostXmlToURL(string url,string data)
{
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
hwr.Method = "POST";
Stream stream = hwr.GetRequestStream();
StreamWri ......
eg:
select * from Exception_Log where DATEDIFF(day,OPER_DATE,getdate())<30 ;
/*
oper_date 为表中查询出来的时间
getdate()为SQLServer里面获得系统时间的函数
含义:当前时间-查询出来的时间小于30天
DateDiff具体用法:
*/
DateDiff 函数
返回两个日期之间的时间间隔。
DateDiff(interval, date1, da ......
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Timers;
using System.Data;
using System.Data.SqlClient;
namespace SMS_joke
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class ......
ASP.NET(C#)返回上一页(后退)代码
2008-08-10 10:32
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["BackUrl"] = Request.UrlReferrer.ToString();
}
}
/// <s ......
有些常见的问题在论坛中不断出现,不妨整理一下。
以下语句是在SQLServer2005上实现的,一些语句无法在SS2000上执行。
有用指数是我根据这个问题的常见程度打的分,仅供参考。实际上,当你遇到了这个问题,这个问题哪怕再少见,解决方案也是非常有用的。
1. 生成若干行记录
有用指数:★★★★★
常见的问题类型:根据 ......