Table of Contents
use master
go
if not exists ( select 1 from sysdatabases where name = 'nHello_dev' )
create database nHello_dev
go
if not exists ( select 1 from sysusers where name = 'testuser' )
begin
print 'create user testuser'
execute sp_addlogin 'testuser', 'showme', 'nHello_dev'
end
go
use nHello_dev
go
if not exists ( select 1 from sysusers where name = 'testuser' )
execute sp_grantdbaccess 'testuser'
goset nocount on
go
if exists ( select 1 from sysobjects where name = 'Message' and type = 'u' )
begin
print 'drop table Message'
drop table Message
end
go
print 'create table Message'
create table Message ( MessageText varchar(50) not null )
go
print 'insert into Message'
insert into Message values ( 'Hello, world!' )
print ltrim(str(@@rowcount)) + ' messages inserted'
goif exists ( select 1 from sysobjects where name = 'p_GetMessage' and type = 'p' )
begin
print 'drop procedure dbo.p_GetMessage'
drop procedure dbo.p_GetMessage
end
go
print 'create procedure dbo.p_GetMessage'
go
create procedure dbo.p_GetMessage as
set nocount on
declare @msg varchar(50)
select @msg = MessageText from Message
if @msg is null
raiserror 99999 'Message table is empty'
else
select @msg
go
print 'grant execute on dbo.p_GetMessage to testuser'
grant execute on dbo.p_GetMessage to testuser
gousing System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace NantSample {
public class MessageServer {
// connstructor
public MessageServer() { }
// get message, using specified config dir
public string GetMessage(string configdir) {
string connstr = GetConnectionString(configdir);
return GetMessageFromDatabase(connstr);
}
// get message, using default config dir (working dir)
public string GetMessage() {
return GetMessage(System.Environment.CurrentDirectory);
}
// get connection string
private string GetConnectionString(string configdir) {
string connstr;
string path = Path.Combine(configdir, "ConnectionString");
try {
using (StreamReader file = new StreamReader(path)) {
connstr = file.ReadLine();
file.Close();
}
}
catch (Exception ex) {
throw new ApplicationException("Error reading connection string: " + ex.Message, ex);
}
return connstr;
}
// get message from database
private string GetMessageFromDatabase(string connstr) {
string msg;
using (SqlConnection conn = new SqlConnection(connstr)) {
try {
conn.Open();
} catch (Exception ex) {
throw new ApplicationException("Error connecting to database: " + ex.Message, ex);
}
using (SqlCommand cmd = new SqlCommand("p_GetMessage", conn)) {
try {
msg = cmd.ExecuteScalar().ToString();
} catch (Exception ex) {
conn.Close();
throw new ApplicationException("Error querying database: " + ex.Message, ex);
}
}
conn.Close();
}
return msg;
}
}
}<object id="svr" class="NantSample.MessageServer" runat="server"/>
<script language="c#" runat="server">
void Page_Load(Object sender, System.EventArgs e) {
string msg;
string configdir = Server.MapPath(".");
string statusdesc = "OK";
int statuscode = 200;
try {
msg = svr.GetMessage(configdir);
} catch (Exception ex) {
msg = "*** " + ex.Message;
statuscode = 500;
statusdesc = ex.Message;
}
Response.StatusCode = statuscode;
Response.StatusDescription = statusdesc;
Response.Write(msg);
}
</script>using System;
using System.IO;
using NantSample;
class TestHarness {
static int Main(string[] argv) {
int rc = 0;
string msg;
try {
MessageServer svr = new MessageServer();
if ( argv.Length > 0 ) {
msg = svr.GetMessage(argv[0]);
} else {
msg = svr.GetMessage();
}
Console.WriteLine(msg);
} catch (Exception ex) {
Console.Error.WriteLine(ex.Message);
rc = 1;
}
return rc;
}
}