PS:自己初学C#,SharePoint时做的一个小tool。

Friday, November 28, 2014

​这个tool编译出来以后可以把部门的daily report保存到本地,数据库,和SharePoint的daily report list中,经过测试能够在我本地的SharePoint2010 SP1环境上正常运行了。

学习C#的第二个小程序,过程中遇到了好多问题,不是很完善,就是意思意思,本次程序练习提取窗体中的信息;向数据库中建表,判断并插入数据;把文件保存到本地指定的路径下;把内容在SharePoint指定list中保存成item并为列赋值。

在此记录一下。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

using Microsoft.SharePoint;

using System.Globalization;

namespace WindowsFormsApplication3

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void SaveToLocalButton_Click(object sender, EventArgs e)

{

SaveToLocal();

}

private void SaveToSQLButton_Click(object sender, EventArgs e)

{

SaveToSQL();

}

private void SaveToWebButton_Click(object sender, EventArgs e)

{

try

{

SaveToWeb();

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

}

private void SaveToAllButton_Click(object sender, EventArgs e)

{

SaveToLocal();

SaveToSQL();

SaveToWeb();

}

private void SaveToLocal()

{

FolderBrowserDialog fbd = new FolderBrowserDialog();

if (fbd.ShowDialog() == DialogResult.OK)

{

string path = fbd.SelectedPath;

MessageBox.Show("Your daily reort will be saved under the path:" + path);

string filename = "DailyReport.doc";

DailyReportText.SaveFile(@path + "/" + filename, RichTextBoxStreamType.TextTextOleObjs);

}

}

private void SaveToSQL()

{

string connectString = @"Data Source=WIN-VVBGFRT65MV\ALWAYSON2;initial catalog=master;uid=sa;pwd=1qaz2wsxE;integrated security=true;pooling=false";

SqlConnection myConnect = new SqlConnection(connectString);

myConnect.Open();

SqlCommand myCommand = myConnect.CreateCommand();

string dateLine = DailyReportText.Lines[0];

int index = dateLine.IndexOf(":");

int lastIndex = dateLine.Length-1;

string date = dateLine.Substring(index+1, lastIndex-index);

myCommand.CommandText =

"use master if exists(select * from dbo.SysObjects where name='DailyReportDB') begin insert into DailyReportDB(date,content) values('"

+ date + "','" + DailyReportText.Text.ToString()+

"') end else begin  create table DailyReportDB(date varchar(1000) primary key,content varchar(1000)) end";

try

{

SqlDataReader myDataReader = myCommand.ExecuteReader();

myDataReader.Close();

}

catch (SqlException)

{

MessageBox.Show("The report of the date you input has already existed in database!");

}

myConnect.Close();

}

private void SaveToWeb()

{

try

{

using (SPSite qaSite = new SPSite("http://win-vvbgfrt65mv"))

{

using (SPWeb qaWeb = qaSite.OpenWeb("/"))

{

string listURL = "http://win-vvbgfrt65mv/Lists/DailyReport";

SPList DailyReportList = qaWeb.GetList(listURL);

SPListItem item = DailyReportList.Items.Add();

//Get the date and time from the form

string dateLine = DailyReportText.Lines[0];

int index = dateLine.IndexOf(":");

int lastIndex = dateLine.Length-1;

string date = dateLine.Substring(index+1, lastIndex-index);

//Get the created by

SPUser currentUser = qaWeb.CurrentUser;

//Take the value into colume

string DataAndTime = date.ToString();

string Description = DailyReportText.Text.ToString();

try

{

string dateFormat = "yyyy/MM/dd";

DateTime SPdate = DateTime.ParseExact(date, dateFormat, CultureInfo.CurrentCulture, DateTimeStyles.None);

//ToShortDateString is important for format

item["Date and Time"] = SPdate.ToShortDateString();

}

catch (Exception)

{

MessageBox.Show("TimeFormat is not right!");

}

try

{

item["Description"] = Description;

}

catch (Exception)

{

MessageBox.Show("Description is not right!");

}

item["Created By"] = currentUser;

try

{

item.Update();

}

catch (Exception ex)

{

MessageBox.Show("Item update Exception!");

MessageBox.Show(ex.ToString());

}

}

}

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

}

}

}

附上一张SharePoint中生成item的图片:

DailyReport自动保存工具的更多相关文章

  1. 【工具】Sublime Text 自动保存功能

    经常需要所以要频繁用到"ctrl+s"保存还是挺麻烦的,所以有的人需要用到失去焦点自动保存功能,这里简单记录下 1.点击"Preferences"里的设置-用户 ...

  2. 微信小程序 vscode 自动保存 保存自动编译 微信开发者工具崩溃

    修改vscode的自动保存延迟时间,将 auto save delay 选项 修改的长一点.

  3. sublime如何自动保存

    sublime是前端开发者喜欢使用的工具,它有很多快捷方式可以让我们快速的编写代码:在开发过程中,每次修改代码之后都要按Ctrl+S保存.在这里向大家介绍一下如何设置让它自动保存. 一.打开subli ...

  4. ArcGIS Add-in——自动保存编辑

    需求:由于初次使用ArcGIS编辑器不习惯.数据量大造成经常程序未响应.计算机断电等因素,造成编辑的数据没有保存,影响了生产效率,本人根据草色静然的博文,总结了自动保存编辑的实现方法. 分析:自动保存 ...

  5. 好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)

    转载:http://www.cnblogs.com/lyhabc/p/3322437.html 挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面) 这个工具主要就是 ...

  6. .NET DLL 保护措施应用实例(百度云批量保存工具)

    最近做了个小工具,将保护措施思路全部应用到了此工具中. 点我下载   百度云批量保存工具是一款专门用于自动批量保存百度云分享的软件. 本软件特点:1:完全模拟人工操作:2:可以批量保存百度分享的文件( ...

  7. iOS 本地自动打包工具

    1.为什么要自动打包工具? 每修改一个问题,测试都让你打包一个上传fir , 你要clean -> 编译打包 -> 上传fir -> 通知测试.而且打包速度好慢,太浪费时间了.如果有 ...

  8. 转:Grunt:任务自动管理工具

    Grunt:任务自动管理工具 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 目录 安装 命令脚本文件Gruntfile.js Gruntfile.js实例:gru ...

  9. 挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)

    原文:挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面) 挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面) 这个工具主要就是自 ...

随机推荐

  1. raid0

    RAID0把数据分割之后放在各个硬盘上,同时读写以提升数据带宽其实在固态硬盘还没有大量出现在零售市场中之前,玩家想要大幅度提升硬盘系统的性能,最常用的方法就是组建RAID0系统.原理很简单,就以SAT ...

  2. vi/vim 键盘图 & 替换

    在VIM中进行文本替换:    1.  替换当前行中的内容:    :s/from/to/    (s即substitude)        :s/from/to/     :  将当前行中的第一个f ...

  3. ajax常用参数

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址.前台跳转到后台 请求参数:前台向后台传数据 回调函数:回调函数就是一个自定义的函数在发生特定的事件的时候调用来处理这个事件 ...

  4. 外部按键 控制 LED 中断 (参考 http://www.oschina.net/question/565065_115196?sort=time )

    转帖: http://www.oschina.net/question/565065_115196?sort=time 实验目的: mini2440开发板上有6个按键,将其中的前4个按键设为外部中断方 ...

  5. 《zw版·ddelphi与halcon系列原创教程》Halcon的短板与delphi

    [<zw版·delphi与Halcon系列原创教程>Halcon的短板与delphi 看过<delphi与Halcon系列>blog的网友都知道,笔者对Halcon一直是非常推 ...

  6. zw版【转发·台湾nvp系列Delphi例程】HALCON ObjToInteger1-4

    zw版[转发·台湾nvp系列Delphi例程]HALCON ObjToInteger1 procedure TForm1.Button1Click(Sender: TObject);var img, ...

  7. yii2表关联实例

    yii2表关联 1.两张表关联,以“商品表关联品牌表”为例 控制器中: $goods_model=new Goods(); $goods_info=$goods_model::find()->j ...

  8. less文件的样式无法生效的一个原因,通过WEB浏览器访问服务器less文件地址返回404错误

    有一种情况容易导致less文件的样式无法生效,就是部分服务器(以IIS居多)会对未知后缀的文件返回404,导致无法正常读取.less文件.解决方案是在服务器中为.less文件配置MIME值为text/ ...

  9. 关于 屏幕阅读器 和 sr-only

    .sr-only = screen reader only 用于读屏器识别使用.

  10. Android侧滑

    人人客户端有一个特效还是挺吸引人的,在主界面手指向右滑动,就可以将菜单展示出来,而主界面会被隐藏大部分,但是仍有左侧的一小部分同菜单一起展示. 据说人人客户端的这个特效是从facebook客户端模仿来 ...