搜集一些C# 或 Sql 中常用代码集锦,学习学习

解决方案 »

  1.   

    T-SQL常见基础疑点问答总结
    http://topic.csdn.net/u/20080728/17/0CDB96BC-9817-4570-8039-FCBA3B156BE4.html
    http://topic.csdn.net/u/20080726/23/45568F53-069F-472E-80B0-1361A72109DE.html
      

  2.   

    SQL操作全集
    http://topic.csdn.net/u/20080515/16/6f359854-5403-4397-89e3-b1b8dd71d527.html
    C#通讯编程(整理)|大家可以进来学习
    http://topic.csdn.net/u/20080623/08/4bbd2475-45f1-42e3-a613-16b094759ade.html
    Asp.NET生成静态页面并分页
    http://topic.csdn.net/u/20080624/10/26A45062-572C-47E9-BB38-D9AA05B6C2ED.html
      

  3.   

    藏经阁第2卷-SQL常用的函数
    http://topic.csdn.net/u/20080803/21/1270777b-a2d6-4323-92d5-cdaa9cf30cf8.html
    实现两个数据库中某个表同步(两台服务器)
    http://topic.csdn.net/u/20080618/10/4c893d16-47fa-468c-9c39-0f316546d0ac.html
    【OpenFlashChart】免费的开源图表项目,图表效果超赞,支持.Net。
    http://topic.csdn.net/u/20090115/22/C3F0D94A-8887-462C-A481-E0C910C88FB2.html
    国内顶级IT公司面试题,答案
    http://topic.csdn.net/u/20090317/18/FBB7DA31-C87E-4FDD-9F15-ECAD63211A35.html
    25 个在 Web 中嵌入图表的免费资源
    http://topic.csdn.net/u/20090414/10/C8DD7E01-CB11-447E-B79E-CDFC2BF62B89.html
    没钱买书的就来下吧 电子书
    http://topic.csdn.net/u/20090414/22/38ba0165-1a6d-46ab-99db-4545c0062505.html
      

  4.   


    SIGNATURE:----------------------------------------------------------------
      

  5.   

    http://blog.csdn.net/hackervs/archive/2008/12/17/3537445.aspx看到了也来贴下
      

  6.   


    /* 字符函数 */
    /* 返回字符表达式中最左侧字符的ASCII代码值 */
    select Ascii('a') --a:97,A:65/* 将整数ASCII代码转换为字符 */
    select Char(97)--97:a,65:A/* 返回表达式中指定字符的开始位置 */
    select Charindex('b','abcdefg',5)/* 以整数返回两个字符表达式的SOUNDEX值之差 */
    select Difference('bet','bit')--3/* 返回字符表达式最左侧指定数目的字符 */
    select Left('abcdefg',3)--abc/* 返回给定字符串表达的字符数 */
    select Len('abcdefg')--7/* 返回将大写字符转换为小字符的字符表达式 */
    select Lower('ABCDEFG')--abcdefg/* 返回删除了前导空格之后字符表达式 */
    select Ltrim('   abcdefg')--abcdefg/* 返回具有给定的整数代码的UNICODE字符 */
    select Nchar(65)--A/* 返回指定表达式中模式第一次出现的开始位置 */
    select Patindex('%_cd%','abcdefg')--2/* 返回为成为有效的SQL SERVER分隔标识符而添加了分隔符的UNICODE字符串 */
    select Quotename('create table')/* 用第三个表达式替换第一个表达式中出现的第二个表达式 */
    select Replace('abcdefg','cd','xxx')--abxxxefg/* 按指定次数重复表达式 */
    select Replicate('abc|',4)--abc|abc|abc|abc|/* 返回字符表达式的逆向表达式 */
    select Reverse('abc')--cba/* 返回字符表达式右侧指定数目的字符 */
    select Right('abcd',3)--bcd/* 返回截断了所有尾随空格之后的字符表达式 */
    select Rtrim('abcd    ')--abcd/* 返回由四个字符表达的SOUNDEX代码 */
    select Soundex('abcd')--A120/* 返回由重复空格组成的字符串 */
    select Space(10)--[          ]/* 返回从默认表达转换而来的字符串 */
    select Str(100)--[       100]/*  */
    select Str(100,3)--[100]/*  */
    select Str(14.4444,5,4)--[14.44]/* 删除指定长度的字符,并在指定的起点处插入另一组字符 */
    select Stuff('abcdefg',2,4,'xxx')--axxxfg/* 返回字符表达式,二进制,文本表达式或图像表达的一部分 */
    select Substring('abcdefg',2,3)--bcd/* 返回表达第一个字符的UNICODE整数值 */
    select Unicode('a')--97/* 返回将小写字符转换为大写字符的字符表达式 */
    select Upper('a')--'A'
      

  7.   


    //某些时候你或许想 "重复使用" 名为 i 的变量,那么定义 Scope 吧。
    {
      var i = 1;
    }{
      var i = 2;
    }
    //类型相同时,和我们一次声明多个同类型变量差不多。
    using (SqlConnection conn = new SqlConnection(), conn2 = new SqlConnection())
    {
    }如果类型不同,那么就写成下面这样。
    using (MemoryStream stream = new MemoryStream())
    using (SqlConnection conn = new SqlConnection())
    {
    }//大家还可以试试这个 
    [Condition("DEBUG")] 
    public void YourDebugStuff() 
    { }
      

  8.   

    总结一下,凡是T-SQL语句都可以...
      

  9.   

    可以坚强勇敢的用来实现串口通信。using System;
    using System.Runtime.InteropServices;
    namespace BusApp
    {
     /// <summary>
     /// 
     /// </summary>
     public class mycom
     {
      public mycom()
      {
       // 
       // TODO: 在此处添加构造函数逻辑
       //
      }
      public int PortNum; //1,2,3,4
      public int BaudRate; //1200,2400,4800,9600
      public byte ByteSize; //8 bits
      public byte Parity; // 0-4=no,odd,even,,space 
      public byte StopBits; // 0,1,2 = 1, 1.5, 2 
      public int ReadTimeout; //10
      
      //comm port win32 file handle
      private int hComm = -1;
      
      public bool Opened = false;
       
      //win32 api constants
      private const uint GENERIC_READ = 0x80000000;
      private const uint GENERIC_WRITE = 0x40000000;
      private const int OPEN_EXISTING = 3;  
      private const int INVALID_HANDLE_VALUE = -1;
      
      [StructLayout(LayoutKind.Sequential)]
       private struct DCB 
      {
       //taken from c struct in platform sdk 
       public int DCBlength;           // sizeof(DCB) 
       public int BaudRate;            // current baud rate 
       public int fBinary;          // binary mode, no EOF check 
       public int fParity;          // enable parity checking 
       public int fOutxCtsFlow;      // CTS output flow control 
       public int fOutxDsrFlow;      // DSR output flow control 
       public int fDtrControl;       // DTR flow control type 
       public int fDsrSensitivity;   // DSR sensitivity 
       public int fTXContinueOnXoff; // XOFF continues Tx 
       public int fOutX;          // XON/XOFF out flow control 
       public int fInX;           // XON/XOFF in flow control 
       public int fErrorChar;     // enable error replacement 
       public int fNull;          // enable null stripping 
       public int fRtsControl;     // RTS flow control 
       public int fAbortOnError;   // abort on error 
       public int fDummy2;        // reserved 
       public ushort wReserved;          // not currently used 
       public ushort XonLim;             // transmit XON threshold 
       public ushort XoffLim;            // transmit XOFF threshold 
       public byte ByteSize;           // number of bits/byte, 4-8 
       public byte Parity;             // 0-4=no,odd,even,,space 
       public byte StopBits;           // 0,1,2 = 1, 1.5, 2 
       public char XonChar;            // Tx and Rx XON character 
       public char XoffChar;           // Tx and Rx XOFF character 
       public char ErrorChar;          // error replacement character 
       public char EofChar;            // end of input character 
       public char EvtChar;            // received event character 
       public ushort wReserved1;         // reserved; do not use 
      }
      [StructLayout(LayoutKind.Sequential)]
       private struct COMMTIMEOUTS 
      {  
       public int ReadIntervalTimeout; 
       public int ReadTotalTimeoutMultiplier; 
       public int ReadTotalTimeoutConstant; 
       public int WriteTotalTimeoutMultiplier; 
       public int WriteTotalTimeoutConstant; 
      }  
      [StructLayout(LayoutKind.Sequential)] 
       private struct OVERLAPPED 
      { 
       public int  Internal; 
       public int  InternalHigh; 
       public int  Offset; 
       public int  OffsetHigh; 
       public int hEvent; 
      }  
      
      [DllImport("kernel32.dll")]
      private static extern int CreateFile(
       string lpFileName,                         // file name
       uint dwDesiredAccess,                      // access mode
       int dwShareMode,                          // share mode
       int lpSecurityAttributes, // SD
       int dwCreationDisposition,                // how to create
       int dwFlagsAndAttributes,                 // file attributes
       int hTemplateFile                        // handle to template file
       );
      [DllImport("kernel32.dll")]
      private static extern bool GetCommState(
       int hFile,  // handle to communications device
       ref DCB lpDCB    // device-control block
       ); 
      [DllImport("kernel32.dll")]
      private static extern bool BuildCommDCB(
       string lpDef,  // device-control string
       ref DCB lpDCB     // device-control block
       );
      [DllImport("kernel32.dll")]
      private static extern bool SetCommState(
       int hFile,  // handle to communications device
       ref DCB lpDCB    // device-control block
       );
      [DllImport("kernel32.dll")]
      private static extern bool GetCommTimeouts(
       int hFile,                  // handle to comm device
       ref COMMTIMEOUTS lpCommTimeouts  // time-out values
       ); 
      [DllImport("kernel32.dll")] 
      private static extern bool SetCommTimeouts(
       int hFile,                  // handle to comm device
       ref COMMTIMEOUTS lpCommTimeouts  // time-out values
       );
      [DllImport("kernel32.dll")]
      private static extern bool ReadFile(
       int hFile,                // handle to file
       byte[] lpBuffer,             // data buffer
       int nNumberOfBytesToRead,  // number of bytes to read
       ref int lpNumberOfBytesRead, // number of bytes read
       ref OVERLAPPED lpOverlapped    // overlapped buffer
       );
      [DllImport("kernel32.dll")] 
      private static extern bool WriteFile(
       int hFile,                    // handle to file
       byte[] lpBuffer,                // data buffer
       int nNumberOfBytesToWrite,     // number of bytes to write
       ref int lpNumberOfBytesWritten,  // number of bytes written
       ref OVERLAPPED lpOverlapped        // overlapped buffer
       );
      [DllImport("kernel32.dll")]
      private static extern bool CloseHandle(
       int hObject   // handle to object
       );
      
      public void Open() 
      {
       
       DCB dcbCommPort = new DCB();
       COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); 
       
       
       // OPEN THE COMM PORT.
        
       hComm = CreateFile("COM" + PortNum ,GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0);
      
       // IF THE PORT CANNOT BE OPENED, BAIL OUT.
       if(hComm == INVALID_HANDLE_VALUE) 
       {
        throw(new ApplicationException("Comm Port Can Not Be Opened"));
       }
      
       // SET THE COMM TIMEOUTS.
       
       GetCommTimeouts(hComm,ref ctoCommPort);
       ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
       ctoCommPort.ReadTotalTimeoutMultiplier = 0;
       ctoCommPort.WriteTotalTimeoutMultiplier = 0;
       ctoCommPort.WriteTotalTimeoutConstant = 0;  
       SetCommTimeouts(hComm,ref ctoCommPort);
      
       // SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
       // THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
       // IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER
       // THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING.
       // ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING.
      
       dcbCommPort.DCBlength = Marshal.SizeOf(dcbCommPort);
       GetCommState(hComm, ref dcbCommPort);
       dcbCommPort.BaudRate=BaudRate;
       dcbCommPort.Parity=Parity;
       dcbCommPort.ByteSize=ByteSize;
       dcbCommPort.StopBits=StopBits;
       SetCommState(hComm, ref dcbCommPort);
        
       Opened = true;
        
      }
      
      public void Close() 
      {
       if (hComm!=INVALID_HANDLE_VALUE) 
       {
        CloseHandle(hComm);
                    Opened=false;
       }
      }
      
      public byte[] Read(int NumBytes) 
      {
       byte[] BufBytes;
       byte[] OutBytes;
       BufBytes = new byte[NumBytes];
       if (hComm!=INVALID_HANDLE_VALUE) 
       {
        OVERLAPPED ovlCommPort = new OVERLAPPED();
        int BytesRead=0;
        ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref ovlCommPort);
        OutBytes = new byte[BytesRead];
        Array.Copy(BufBytes,OutBytes,BytesRead);
       } 
       else 
       {
        throw(new ApplicationException("Comm Port Not Open"));
       }
       return OutBytes;
      }
      
      public int Write(byte[] WriteBytes) 
      {
       int BytesWritten = 0;
       if (hComm!=INVALID_HANDLE_VALUE) 
       {
        OVERLAPPED ovlCommPort = new OVERLAPPED();
        WriteFile(hComm,WriteBytes,WriteBytes.Length,ref BytesWritten,ref ovlCommPort);
       }
       else 
       {
        throw(new ApplicationException("Comm Port Not Open"));
       }  
       return BytesWritten;
      }
     }
    }
      

  10.   

    g.cn
    上面很多很多东西用到才去了解的,因为本来就是又很多变化的不同需求,不同做法
      

  11.   

     // "uid=sa":连接数据库的用户名为sa.
                // "password=":连接数据库的验证密码为空.他的别名为"pwd",所以我们可以写为"pwd=".
                // "initial catalog=Northwind":使用的数据源为"Northwind"这个数据库.他的别名为"Database",本句可以写成"Database=Northwind".
                // "Server=YourSQLServer":使用名为"YourSQLServer"的服务器.他的别名为"Data Source","Address","Addr".
                // " Connect Timeout=30":连接超时时间为30秒.(根据情况添加)
                // PS:
                //  1.你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用"uid"和"password"这样的方式来登录,而需要使用"Trusted_Connection=SSPI"来进行登录.
                //  2. 如果使用的是本地数据库且定义了实例名,则可以写为"Server=(local)\实例名";如果是远程服务器,则将"(local)"替换为远程服务器的名称或IP地址.
                string strConnection = "Trusted_Connection=SSPI;";
                strConnection += "database=NTF_Navision_enlistment60;Server=CAIXIATA-6BE823;";
                strConnection += "Connect Timeout=30";
    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LCL_data/archive/2009/04/30/4139145.aspx
      

  12.   


    /*
    分页查询
    */
    create PROCEDURE [sp_Page]
    @PageCurrent int, --要显示的页码
    @PageSize int,--每页的大小
    @ifelse nvarchar(1000),--返回的字段
    @where nvarchar(1000),--条件
    @order nvarchar(1000)--排序
    AS
    BEGIN
    BEGIN TRY
    DECLARE @strSQL nvarchar(500),@strSQLcount nvarchar(500),@countPage int
    if ISNULL(@PageSize,0)<1 
    set @PageSize=20
    set @strSQL='select * from
    (
    select '+@ifelse+',ROW_NUMBER() OVER('+@order+') as row from '+@where+'
    ) a 
    where row between '+CONVERT(nvarchar(10), @PageCurrent*@PageSize-@PageSize+1)+' and '+CONVERT(nvarchar(10), @PageCurrent*@PageSize)+''
    set @strSQLcount='select count(1) as count from '+@where
    EXECUTE sp_executesql @strSQL;
    EXECUTE sp_executesql @strSQLcount;
    END TRY 
    BEGIN CATCH
    print ERROR_MESSAGE()
    END CATCH
    END
      

  13.   


    ---测试数据---
    if object_id('[pactinfo]') is not null drop table [pactinfo]
    go
    create table [pactinfo]([ID] int,[pactname] varchar(4))
    insert [pactinfo]
    select 1,'正常' union all
    select 2,'中国' union all
    select 3,'做饭' union all
    select 4,'加发'
    go---引用前辈们的一个函数---
    create function   f_GetPy(@str   nvarchar(4000)) 
    returns   nvarchar(4000) 
    as 
    begin 
    declare   @strlen   int,@re   nvarchar(4000) 
    declare   @t   table(chr   nchar(1)   collate   Chinese_PRC_CI_AS,letter   nchar(1)) 
    insert   into   @t(chr,letter) 
        select   '吖 ', 'A '   union   all   select   '八 ', 'B '   union   all 
        select   '嚓 ', 'C '   union   all   select   '咑 ', 'D '   union   all 
        select   '妸 ', 'E '   union   all   select   '发 ', 'F '   union   all 
        select   '旮 ', 'G '   union   all   select   '铪 ', 'H '   union   all 
        select   '丌 ', 'J '   union   all   select   '咔 ', 'K '   union   all 
        select   '垃 ', 'L '   union   all   select   '嘸 ', 'M '   union   all 
        select   '拏 ', 'N '   union   all   select   '噢 ', 'O '   union   all 
        select   '妑 ', 'P '   union   all   select   '七 ', 'Q '   union   all 
        select   '呥 ', 'R '   union   all   select   '仨 ', 'S '   union   all 
        select   '他 ', 'T '   union   all   select   '屲 ', 'W '   union   all 
        select   '夕 ', 'X '   union   all   select   '丫 ', 'Y '   union   all 
        select   '帀 ', 'Z ' 
        select   @strlen=len(@str),@re= ' ' 
        while   @strlen> 0 
        begin 
            select   top   1   @re=letter+@re,@strlen=@strlen-1 
                from   @t   a   where   chr <=substring(@str,@strlen,1) 
                order   by   chr   desc 
            if   @@rowcount=0 
                select   @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1 
        end 
        return(@re) 
    end 
    go---查询---
    select 
      * 
    from 
      [pactinfo]
    where
      left(dbo.f_GetPy(pactname),2)='zf'
      

  14.   

    SQL学的不好,来学习一下,看完代码后发现,我需要去恶补一下!!!
      

  15.   


    这也太准了,我就是在海淀,用的确实是WINdows2003,还是火狐浏览器,除了IP和该行错了之外,其他全中!!!   (__) 
      /oo\\________
      \ /     \---\
       \/    /  \  \
        \\_|___\\_|/  *
          ||  YY|  
          ||  ||