①
string strconn = "user id=sa;password=;initial catalog=starmad;server=stnew;Connect TimeOut=60 "; SqlConnection conn = new SqlConnection(strconn); conn.Open();
②
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
class Test
{
static void Main()
{
MessageBox.Show(GetServerTime().ToString( "yyyy.MM.dd HH:mm.ss "));
}
static DateTime GetServerTime()
{
string strConn = "server=服务器IP;uid=sa;pwd=数据库密码; ";
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlCommand cmd = new SqlCommand( "select getdate() ", conn);
conn.Open();
return (DateTime)cmd.ExecuteScalar();
}
}
}