(1)如何判断2个字符串对象是否指向同一个引用?
string s1 = "aa";string s2 = "aa";MessageBox.Show(object.ReferenceEquals(s1,s2).ToString()); //true
char[] ch = { 'a', 'a' };string s1 = new string(ch);string s2 = "aa";MessageBox.Show(object.ReferenceEquals(s1,s2).ToString()); //false(2)获取本机IP地址。 public static IPAddress[] GetLocalhostIPv4Addresses() { String LocalhostName = Dns.GetHostName(); IPHostEntry host = Dns.GetHostEntry(LocalhostName); List<IPAddress> addresses=new List<IPAddress>(); foreach (IPAddress ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) addresses.Add(ip); } return addresses.ToArray(); }
(3)如何把窗体坐标转换成屏幕坐标? private void Form4_MouseMove(object sender, MouseEventArgs e) { textBox1.Text = string.Format("x:{0},y:{1}", e.X,e.Y); Point p = new Point(e.X, e.Y); p=this.PointToScreen(p); //把窗体坐标转换成屏幕坐标 textBox2.Text = string.Format("x:{0},y:{1}", p.X,p.Y); p = this.PointToClient(p); textBox3.Text = string.Format("x:{0},y:{1}", p.X, p.Y); }(4) 窗体显示帮助按钮this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;this.HelpButton = true;this.MaximizeBox = false;this.MinimizeBox = false;点击帮助按钮,显示chm格式的帮助文件?
System.Diagnostics.Process.Start("ArticleCollectorAPI.chm");
(5)控件的Anchor、Dock属性区别Anchor用于设置控件与窗体边缘的距离保持不变!Dock用于设置控件紧贴窗体的某个边缘或者填充整个窗体。
(6)dgvStockList_CellStateChanged事件作用?
在单元格的状态更改时(例如,当单元格失去或获得焦点时)发生。 要触发该事件,SelectionMode属性不能用FullRowSelect,要用CellSelect.DataGridView.CellValueChanged事件作用?
CellValueChanged事件在单元格的值改变,并且是在焦点离开单元格时发生。用代码绑定数据时,也会触发该事件!(7)DataGridView控件在最左边列显示序号!!///简单的方法:绑定数据后,用下面的代码int rowNumber = 1; foreach (DataGridViewRow row in dataGridView1.Rows){ row.HeaderCell.Value = rowNumber.ToString(); rowNumber++;
}但序号是2位数的话,显示不清楚,列宽不够。要修改DataGridView控件的RowHeadersWidth属性。第2种方法,能让箭头都不显示 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { Rectangle rowHeaderBounds = new Rectangle ( 2, e.RowBounds.Top, this.dataGridView1.RowHeadersWidth - 2, e.RowBounds.Height - 1 );
using (Brush backbrush = new SolidBrush(SystemColors.Control)) { e.Graphics.FillRectangle(backbrush, rowHeaderBounds); }
if (e.RowIndex >= dataGridView1.FirstDisplayedScrollingRowIndex) { using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)) { int linen = 0; linen = e.RowIndex + 1; string line = linen.ToString(); e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5); SolidBrush B = new SolidBrush(Color.Red); } }
} }
(8)使用DataGridView显示、更新、删除数据
string connstr = @"server=./sqlexpress;initial catalog=MySchool;uid=sa;pwd=sa2008"; DataSet ds = new DataSet();SqlDataAdapter da;
//显示数据
private void LoadData(){ string strwhere; if (comboBox1.Text == "男") strwhere="sex='男'"; else if (comboBox1.Text == "女") strwhere="sex='女'"; else strwhere="sex='男' or sex='女'"; string sql = "select StudentId,StudentNo,StudentName,Sex from Student where " + strwhere; da = new SqlDataAdapter(sql, connstr);
ds.Tables.Clear(); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0];
}//更新数据
private void button2_Click(object sender, EventArgs e) {
//自动产生更新数据库的SQL语句,但其数据必须是来自同一个数据表。否则,更新失败! SqlCommandBuilder cmdb = new SqlCommandBuilder(da);
da.Update(ds); LoadData(); }
//删除数据
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count== 0) //SelectionMode属性用FullRowSelect { MessageBox.Show("请选择!"); return; } if (MessageBox.Show("确认要删除吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { using (SqlConnection conn = new SqlConnection(connstr)) { string sql = "delete from Student where StudentId=" + (int)dataGridView1.SelectedRows[0].Cells[0].Value; SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); cmd.ExecuteNonQuery(); } LoadData(); } }
(9)向SQL Server数据库中的日期字段插入值:
string strconn = @"server=./student;database=jxgl;uid=sa;pwd=123456"; DateTime t; if(!DateTime.TryParse(textBox3.Text,out t)) //检查日期格式是否正确 { MessageBox.Show("日期出入有误,请修改"); return; } using (SqlConnection conn = new SqlConnection(strconn)) { string sql = string.Format("insert into student_info(stud_id,stud_name,enter_date) values('{0}','{1}','{2}')", textBox1.Text, textBox2.Text, t); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); cmd.ExecuteNonQuery(); }
由此可知,SQL Server是把日期字段当作字符串类型来处理的。所以只要日期格式正确的话,不用转换,直接用文本框中的值插入数据库中也是可行的。即sql语句可以为:
string sql = string.Format("insert into student_info(stud_id,stud_name,enter_date) values('{0}','{1}','{2}')", textBox1.Text, textBox2.Text, textBox3.Text); 当然这种利用字符串拼接产生sql语句的方法并不推荐!
如果用DateTimePicker控件,sql语句可以为:
string sql = string.Format("insert into student_info(stud_id,stud_name,enter_date) values('{0}','{1}','{2}')", textBox1.Text, textBox2.Text, dateTimePicker1.Value);
查询某个时间段内的记录可以用如下的代码:
using (SqlConnection conn = new SqlConnection(strconn)) { string sql = string.Format("select * from student_info where enter_date between '{0}' and '{1}'", dateTimePicker2.Value, dateTimePicker3.Value); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); using (SqlDataReader rd = cmd.ExecuteReader()) { DataTable dt = new DataTable(); dt.Load(rd); dataGridView1.DataSource = dt; } }(10)操作Access日期字段:
插入数据的sql语句:
string sql = string.Format("insert into test(name,enterdate) values('{0}',#{1}#)", textBox2.Text, textBox3.Text);
查询某个时间段内的记录可以用如下的sql语句: string sql = string.Format("select * from test where enterdate between #{0}# and #{1}#", dateTimePicker1.Value.ToShortDateString(), dateTimePicker3.Value.ToShortDateString());