C#资料一、数据库相关连接字符串:Data Source=myServerAddress;Initial Catalog=myDataBase;UserId=myUsername;Password=myPassword;Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;InitialCatalog=myDataBase;User ID=myUsername;Password=myPassword;string sqlConnection="DATA SOURCE=(local);DATABASE=DB;USER ID=SA;PWD=123";public static readonly string connstr = "Data Source=vmb72;Initial Catalog=SCHOOL;Integrated Security=True;Connect Timeout=15";"Data Source=C:\BegASPNET\Northwind.mdb"是指明数据源的位置,他的标准形式是"Data Source=MyDrive:MyPath\MyFile.MDB". PS: 1."+="后面的"@"符号是防止将后面字符串中的"\"解析为转义字符. 2.如果要连接的数据库文件和当前文件在同一个目录下,还可以使用如下的方法连接: strConnection+="Data Source=";strConnection+=MapPath("Northwind.mdb"; 这样就可以省得你写一大堆东西了!3.要注意连接字符串中的参数之间要用分号来分隔. "OleDbConnectionobjConnection=new OleDbConnection(strConnection);"这一句是利用定义好的连接字符串来建立了一个链接对象,以后对数据库的操作我们都要和这个对象打交道.打开连接:SqlConnection conn = new SqlConnection(connstr);conn.Open();操作:SqlCommand sqlcom=new SqlCommand("insert into info(id,name,sex)values("+i+",'"+s 1+"','"+s2+"')",objSqlConnection);sqlcom.ExecuteNonQuery();填充:DataTable dt1=new DataTable();SqlDataAdapter da1=new SqlDataAdapter();string query="SELECT*from info where id="+i;DataSet objDataSet=new DataSet();SqlDataAdapter obj=new SqlDataAdapter();obj.SelectCommand=new SqlCommand(query,objSqlConnection);obj.Fill(objDataSet,"info");SqlCommand objSqlCommand=new SqlCommand(query,objSqlConnection);SqlDataRe ader objSqlReader=objSqlCommand.ExecuteReader();数据库连接示例:static public SqlConnection GetConn(){string connString = null;connString =System.Configuration.ConfigurationManager.AppSettings["ConnString"]; SqlConnection conn = new SqlConnection(connString);return conn;}// 执行select语句,返回DataTable对象static public DataTable QueryBySql(string sqlStr){DataTable dt = null;SqlConnection conn = null;SqlDataAdapter da = null;try{dt = new DataTable();conn = GetConn();da = new SqlDataAdapter(sqlStr, conn);da.Fill(dt);}catch (Exception ex){throw ex;}finally{dt.Dispose();da.Dispose();conn.close();conn.Dispose();}return dt;}// 执行insert into、update、deletestatic public int SqlCmdOperation(string sqlText){SqlConnection conn = null;SqlCommand cmd = null;int count = 0;try{conn = GetConn();cmd = new SqlCommand(sqlText, conn);if (conn.State != ConnectionState.Open)conn.Open();count = cmd.ExecuteNonQuery();}catch (Exception ex){throw (ex);}finally{cmd.Dispose();conn.Close();conn.Dispose();}return count;}二、l istview1.listview更新LvCourse.Items.Clear();DataTable CourerInfo = SqlHelper.ExecuteDataTable("select * from dbo.Course order by Cno");LvCourse.BeginUpdate();for (int i = 0; i < CourerInfo.Rows.Count; i++){DataRow Course = CourerInfo.Rows[i];ListViewItem Cou = new ListViewItem();Cou.Text = Course["Cno"].ToString(); //设置第一列Cou.SubItems.Add(Course["Cname"].ToString());//设置其他列Cou.SubItems.Add(Course["Cpno"].ToString());Cou.SubItems.Add(Course["Ccredit"].ToString());LvCourse.Items.Add(Cou);}LvCourse.EndUpdate();向listview中填充数据,应先把listview view属性设置为details。
Fullrowselect属性可以设置为true(整行选择)。
Gridlines属性设置为true时listview中显示表格线。
2.listview选中行private void LvStudent_SelectedIndexChanged(object sender, EventArgs e){if (LvStudent.SelectedItems.Count > 0){i = Convert.ToInt32(LvStudent.SelectedItems[0].Index.ToString());TBSno.Text = LvStudent.Items[i].SubItems[0].Text;TBSname.Text = LvStudent.Items[i].SubItems[1].Text;TBSsex.Text = LvStudent.Items[i].SubItems[2].Text;TBSage.Text = LvStudent.Items[i].SubItems[3].Text;TBSdept.Text = LvStudent.Items[i].SubItems[4].Text;}}3.其他提示框:DialogResult res = MessageBox.Show("确定要删除学号为" + TBSno.Text.Trim()+ "的学生?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);if (res == DialogResult.Yes){}点击按钮:btnRefreshGrade.PerformClick();三、文件操作1.选择目录FolderBrowserDialog fbd = new FolderBrowserDialog();fbd.Description = "请选择目录";fbd.RootFolder = Environment.SpecialFolder.MyComputer;if (fbd.ShowDialog() == DialogResult.OK){}2.填充listviewlistView1.Clear();try{foreach (string dirname in Directory.GetDirectories(dirfilename)){ListViewItem itemlist = new ListViewItem(dirname);listView1.Items.Add(itemlist);}foreach (string filename in Directory.GetFiles(dirfilename)){ListViewItem itemlist = new ListViewItem(filename);listView1.Items.Add(itemlist);}}catch { }应将listview view属性设置为list;代码创建listview列标头1.ColumnHeaderch= new ColumnHeader();2.3.ch.Text = "列标题1"; //设置列标题4.5.ch.Width = 120;//设置列宽度6.7.ch.TextAlign = HorizontalAlignment.Left; //设置列的对齐方式8.9.this.listView1.Columns.Add(ch);//将列头添加到ListView控件。